1. Invert and reverse bits.

Hello,

I need to invert and reverse the bits in a one byte value. For example:

00111111 original
11000000 inverted
00000011 reversed

Here is what I have. It works, but I was wondering if there is a better or faster way.

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

int invert(int temp);

int main()
{
int a = 0x3f;
cout << hex << a << endl;
cout << hex << invert(a) << endl;

return 0;
}

int invert(int temp)
{
int res         = 0;          //Result

temp = 0xff - temp;     // ones complement?

for(int x = 7; x > 0 ; x--)
{
res = res | (temp & 0x01);
temp    = temp  >> 1;
res       = res     << 1;
}
res = res | (temp & 0x01);

return res;
}```

2. Inverting bits is easy:

MyVar = ~MyVar;

3. There's a ~ operator for this:

Code:
```int x = 0xA5A5A5A5;
int y = ~x;

printf("%x:%x\n", x, y);```

4. I didn't read the part about reversing... there's no easy way for that, although it would probably be easiest to use the << and >> operators. The following might work, I didn't test it.

Code:
```x = 0x0F0F0F0F;
y = 0;
for(i=0; i<32; ++i) // All 32 bits
{
y |= (x & 0x1);
y << 1;
x >> 1;
}```

5. Ignore that last post. I should probably register so I can edit. I didn't think that through enough.

Anyway, this program will reverse the bits in a 32-bit number:

Code:
```#include <cstdio>

int main(int argc, char *argv[])
{
int x = 0xF0F0F0F0;
int y = 0;
int i;

for(i=0; i<32; ++i)
{
y = y << 1;
y |= (x & 0x1);
x = x >> 1;
}
printf("Y = %x\n", y);

return 0;
}```