# Bits

• 09-05-2009
dpp
Bits
I was searching for an algorithm to reverse the bits...
I found one and coded it in c++...will this work
Code:

```#include<iostream> using namespace std; int main() { unsigned int num; cin>>num;            // Reverse the bits in this number. unsigned int temp = num;    // temp will have the reversed bits of num. int i; char xxx[10]; for (i = (sizeof(num)*8-1); i; i--) {   temp = temp | (num & 1);  temp <<= 1;    num  >>= 1;   } temp = temp | (num & 1); cout<<temp<<"final"; return 0; }```
• 09-05-2009
nathanpc
First of all, read more about bits, then you will see if your code will work or not.
Wikipedia - Bit
• 09-05-2009
dpp
Quote:

Originally Posted by nathanpc
First of all, read more about bits, then you will see if your code will work or not.
Wikipedia - Bit

S i do know the basics... but here in teh code pasted num value is divided by 2 at each iteration and reaches zero...(since right shift)
temp value multiplying all the time(since leftshift)
i don't see anything for reversal as far as i know
that is why am here
• 09-05-2009
vart
Quote:

Originally Posted by dpp
temp value multiplying all the time(since leftshift)
i don't see anything for reversal as far as i know
that is why am here

forget about multiplication and division here

we just moving bits from one number to the right - and taking every time the least significant one, then moving it to another number, shifting what is there to the left...

look at it as if you have 32 blocks with 0 or 1 written on it - you are taking one from the right and adding it to another column...

when you are finished - you have same 32 blocks ordered in the opposite direction
• 09-05-2009
abachler
Code:

```// reverse bits in a byte unsigned char A, B; A = 170; // the number to be reversed B = 0; for(int x = 0;x<8;x++){   B << 1;   B += A & 1;   A >> 1;   }```
• 09-07-2009
dpp
Quote:

Originally Posted by vart
forget about multiplication and division here

we just moving bits from one number to the right - and taking every time the least significant one, then moving it to another number, shifting what is there to the left...

look at it as if you have 32 blocks with 0 or 1 written on it - you are taking one from the right and adding it to another column...

when you are finished - you have same 32 blocks ordered in the opposite direction

I don't undesrtand the code with your explanation completely...
will u be patient enough to explain me in detail
Thousands of thanks ;)
• 09-07-2009
Cogman
In c++ the >> is the bitshift operator. While it is true that a bitshift is equivalent to a / or * by 2 in binary, it should not be thought of that way.

For example lets say that
short x = 8;
short y = 9;

thats like saying
x = 0x1000
y = 0x1001

x >> 1 is
0x0100
x >> 2 is
0x0010
x >> 3 is
0x0001

and y << 1 is
0x0010
y << 2 is
0x0100
and y << 3 is
0x1000

notice how it is shifting. Maybe to be more expressive, a base ten shift would look like this.

234 << 1 = 340
234 >> 1 = 023

effectively dividing and multiplying the number by 10.

hope that makes sense.
• 09-08-2009
iMalc
For more practical purposes, I prefer this method:
Bit Twiddling Hacks