How to reverse bits

Pages: 12
Sep 3, 2013 at 6:42pm
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!!
Sep 3, 2013 at 6:53pm
x ^= x?
Sep 3, 2013 at 6:57pm
Can you explain this?
Sep 3, 2013 at 7:00pm
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.
Sep 3, 2013 at 7:10pm
I'm sorry. You need to use a mask.

1
2
3
4
int a = 5;
int b = 0xFFFFFFFFFFFFFFFF;

a ^= b;


That should get you what you're looking for.
Sep 3, 2013 at 7:27pm
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 Sep 3, 2013 at 7:29pm
Sep 3, 2013 at 7:32pm
Check this out:
1
2
3
4
5
6
7
8
9
10
11
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 Sep 3, 2013 at 7:34pm
Sep 3, 2013 at 7:39pm
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.
Sep 3, 2013 at 8:00pm
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
Sep 3, 2013 at 8:06pm
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
Sep 3, 2013 at 8:11pm
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 Sep 3, 2013 at 8:36pm
Sep 3, 2013 at 8:28pm
@ciphermagi
If you read the OP again you'd see that Stewbond is right.

http://ideone.com/jcz4oF
Last edited on Sep 3, 2013 at 8:36pm
Sep 3, 2013 at 8:35pm
@Caligulaminus:
Check the links that cire and I put up.
Sep 3, 2013 at 8:36pm
Sep 3, 2013 at 8:37pm
Stewbond's code is correct, ciphermagi.
Last edited on Sep 3, 2013 at 8:38pm
Sep 3, 2013 at 8:39pm
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.
Sep 3, 2013 at 8:40pm
Though I'd use a template (C++11)

http://ideone.com/gO6y72
Sep 3, 2013 at 8:49pm
Thanks for all input. But will htons or ntohs work as well.
Sep 3, 2013 at 8:52pm
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.)
Sep 3, 2013 at 8:56pm
Cire,
I did test on a several numbers and passed on all except 2 cases.
Pages: 12