I’m sorry if this seems like a stupid question I’m just having trouble understanding bits and bitwise operation assignment
I have two integers one is a mask and the other is arbitrary and I’m supposed to xor the integer with the 16 most significant bits of the mask but I’m not exactly sure if significant is the first 16 or the last 16 and if my operation is even correct since I don’t know how to verify it
I tried this
int main(){
uint32_t mask = 3405705229;
uint16_t arbitrary = 0xABCD;
arbitrary^=mask&16;
printf("%X\n",arbitrary);
}
I assumed mask&16 would give me only the first 16 bits of mask
But when I print I still get ABCD so that can’t be right..
I also tried arbitrary^=(mask>>16)&16; but that didn’t do anything either
>Solution :
For an unsigned 8-bit value, you might have:
Bit number: 7 6 5 4 3 2 1 0
Bit value: 1 0 0 0 1 1 0 1 = 141 = 0x8D
The 4 most significant bits (MSB) are bits 7-4; the 4 least significant bits (LSB) are bits 3-0.
You’d extract the 4 most significant bits from uint8_t x8 = 141; using:
uint8_t y = (x >> 4) & 0xF
The output would be y equal to 8 or 0x08.
You can, of course, expand this to accommodate larger numbers of bits.