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!!
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:
1 2 3
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.
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.
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.