### How to reverse bits

Pages: 12
Hello,
I am working on a project where I need to reverse bits for a number.
For example, if I have 110111100000000 I need to reverse it to 0000000001111011.
I am straight clueless on how to do this. Any help will be greatly appreciated!!
x ^= x?
Can you explain this?
It's a unary bit operator. The carat (^) means exclusive or. So set x equal to the exclusive or of x. I'm not sure precisely, but I think that will flip your bits entirely.

I know that this is true:
 ``123`` ``````a ^= b; b ^= a; a ^= b;``````

The above code will swap values between a and b, so if a was 5 before, and b was 98732636, then b will be 5, and a will be 98732636.
I'm sorry. You need to use a mask.

 ``1234`` ``````int a = 5; int b = 0xFFFFFFFFFFFFFFFF; a ^= b;``````

That should get you what you're looking for.
I think he's trying to swap endianness wrong term. He wants the least-significant bit to be swapped with the most significant bit.
Last edited on
Check this out:
 ``1234567891011`` ``````unsigned int ReverseBits(unsigned int input) { unsigned int output = input; for (int i = sizeof(input) * 8-1; i; --i) { output <<= 1; input >>= 1; output |= input & 1; } return output; }``````
Last edited on
Okay, Stewbond. First off, your method doesn't reverse anything, it only moves them over and then sets the smallest significant to true. He will need a mask.

Secondly, I'm not in the habit of answering homework questions outright, so I gave him some examples of how to do it, but obviously not specific to his homework problem. If he can't figure it out based on what's already there, then he's got to look a little closer into how he's handling his code.
 Okay, Stewbond. First off, your method doesn't reverse anything, it only moves them over and then sets the smallest significant to true. He will need a mask.

You might want to actually trace through the code.

http://ideone.com/aikPPf
 You might want to actually trace through the code.

You should know better than to trust an input that's that symmetric.

http://ideone.com/GeNIRR
So it seems I should've traced through more carefully. ;)

Actually, it looks like I should've just paid more attention when I came back to this. The OP requires the bits are reversed, and Stewbond's code does that, but for some reason when I looked at your link I was thinking the bits should be "flipped" which is obviously not the case.
Last edited on
@ciphermagi
If you read the OP again you'd see that Stewbond is right.

http://ideone.com/jcz4oF
Last edited on
@Caligulaminus:
Check the links that cire and I put up.
Stewbond's code is correct, ciphermagi.
Last edited on
I see....I was evaluating them as inverted when I looked at the links.

I still vehemently disagree with finishing a homework problem without actually helping them to understand. Not budging on that.
Though I'd use a template (C++11)

http://ideone.com/gO6y72
Thanks for all input. But will htons or ntohs work as well.
 Thanks for all input. But will htons or ntohs work as well.

Is there some reason you can't test that for yourself?

(No, they will not work.)
Cire,
I did test on a several numbers and passed on all except 2 cases.
Pages: 12