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; }