How to reverse bit of a number (k bits) using bitwise? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C reverse bits in unsigned integer
How does this code work to reverse bits in number?
Given a number n with k bits, (n < 2^k), is there a fast way to do it using bitwise? This is my slow solution:
int reverse_bit(int n, int bit_size) {
bit_size--;
int result = 0;
while (n) {
if ((n & 1) == 1)
result += 1 * (1 << bit_size);
n >>= 1;
bit_size--;
}
return result;
}

Related

What n&1 and n>>=1 mean? [duplicate]

This question already has answers here:
What is (x & 1) and (x >>= 1)?
(5 answers)
Closed 2 years ago.
I was doing this exercise : https://www.hackerrank.com/challenges/30-binary-numbers/problem and I found this code, but I didn't understand what the condition with n&1 and n>>=1 do here.
//C++ program to convert a decimal
// number to binary number
#include <iostream>
using namespace std;
int main()
{
int n,count=0,max=0;
cin >> n;
while(n)
{
if (n&1)
count++;
else
count = 0;
if (max < count)
max = count;
n>>=1;
}
cout << max;
return 0;
}
if (n&1)
checks whether n is odd by doing a bitwise and.
n>>=1;
shifts the bits of n to the right by one bit.
The & is a bitwise-AND operator and evaluates the expression in either true or false (when the expression is conditional), it's pretty much similar to x % 2, i.e. this condition:
if (n & 1) {
//...
}
// is equal to
if (n % 2) {
// ...
}
OTOH, n >>= 1 shifts right n by a bit.

Numbers larger than long long [duplicate]

This question already has answers here:
Adding numbers larger than long long in C++
(4 answers)
Closed 5 years ago.
I am writing a C++ program to generate the series of Fibonacci numbers. This is the 1, 1, 2, 3, 5... series. The 300th number in this series is 359579325206583560961765665172189099052367214309267232255589801. This is well beyond the limits of int or even unsigned long long. How can I continue to represent such large numbers?
Here's my code:
unsigned long long FibLoop(int n)
{
// Keep track of previous two numbers
unsigned long long prev[2];
prev[0] = 1;
prev[1] = 1;
// Loop
for(int i = 2; i <= n; i++)
{
prev[i % 2] = prev[0] + prev[1];
cout << i << "\t" << prev[i % 2] << endl;
}
// Return
return prev[n % 2];
}
You need to download from external libraries such as BoostMultiprecision

Wrap negative index to size of array [duplicate]

This question already has answers here:
Is there an expression using modulo to do backwards wrap-around ("reverse overflow")?
(6 answers)
Closed 5 years ago.
Given an array of length size and an index n to that array, how can I wrap the index such that it is always within size-1? For positive numbers its simply n % size, but how to achieve backwards wrap around when n is negative?
What I've come up with:
int wrap(int size, int n) {
if (n >= 0)
return n % size;
else
return abs(size + n) % size;
}
But this only works for n <= size; How to make it work for any n?
Expected output:
wrap(4, -1) == 3
wrap(4, -2) == 2
wrap(4, -3) == 1
wrap(4, -4) == 0
wrap(4, -5) == 3
wrap(4, -6) == 2
wrap(4, -7) == 1
wrap(5, -8) == 0
You want the modulo operator with floored division.
int mod_floor(int a, int n) {
return ((a % n) + n) % n;
}

How can we achieve last e.g. 6 digits of of Nth Fibonacci number in O(logN) time? [duplicate]

This question already has answers here:
nth fibonacci number in sublinear time
(16 answers)
Closed 6 years ago.
I have seen a task on an online test with competitive programming challenges (cannot disclose unfortunately where) to produce last (least significant) 6 digits of Nth Fibonacci number.
I have managed to come up with the following solution:
#include <iostream>
#include <cassert>
#include <tuple>
int solution(int N)
{
if(N == 0) return 0;
if(N == 1) return 1;
if(N == 2) return 1;
int a = 0;
int b = 1;
int c = 1;
for (int i = 3; i <= N; ++i) {
std::tie(a, b, c) = std::make_tuple(b, (a + b) % 1000000, (b + c) % 1000000);
}
return c;
}
int main()
{
assert(solution(8) == 21);
assert(solution(36) == 930352);
std::cout << solution(10000000) << std::endl;
}
which unfortunately has O(N) time complexity and start to run quite slow for inputs like in the last line: N > 10000000.
Anyone knows how this can be achieved in O(logN)?
There is an algorithm taking O(log_n) time to compute nth Fibonacci number using Q-Matrix. You can take a look at http://kukuruku.co/hub/algorithms/the-nth-fibonacci-number-in-olog-n, the only change you will need is to make sure it produce only last 6 digits.

Compare two binary numbers and get the different bits [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best algorithm to count the number of set bits in a 32-bit integer?
I want to write a program to get the number of 1's bit in comparing two numbers.if I compare the bits between any two numbers
to find where the binary numbers are different in the 1's and 0's.
in other words Exclusive OR (XOR) relationship.
like if 22 (which has 10110 binary)and compare it with 15 (which has 01111 binary)
the first one 10110
the second one 01111
the result 11001
and the answer would be 25 but what I want to get is 3 where there is three 1's and 0's that are different.
Hrmmm, the first non-recursive idea that comes to mind is:
int a = ...;
int b = ...;
int x = a ^ b;
int count;
for (int i = 0; i < 32; ++i) {
if (x & (1 << i)) {
++count;
}
}
std::bitset::count should do what you're looking for:
http://www.cplusplus.com/reference/stl/bitset/count/
http://en.cppreference.com/w/cpp/utility/bitset/count
This would be the easiest approach:
int count_bits(int a)
{
int mask;
int count = 0;
for (mask = 1; mask; mask <<= 1)
{
if (mask & a) ++count;
}
return count;
}
If you want something more fancy take a look here: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive