-
bitwise question
If I have the first byte of an int set to 00111011, where the last two ones are flags, how do I get the first 6 numbers and then convert them to a number so I can print it out. EX:
I have 00111011 and I want to get the 001110 out of it, and change that to its own number, without getting/changing the last two ones. I hope this doesn't seem too complicated. Thanks!
-
If the other bytes of your integer are 0s, then all you need to do is this:
int a = 0x0000003b;
int b;
b = a >> 2; //shift right 2 bits
cout << hex << b << endl;
If you need to mask the higher bytes, then do this:
int a = 0xffffff3b;
b = (a & 0xff) >> 2;
cout << hex << b << endl;
Or use all shifts:
b = a << 24 >> 26; //shift left 24 bits, then right 26 bits
This assumes your integer is 4 bytes. To find this out, print out the value of sizeof(int).