Given the register value 0b11111111, what is the value of reg after clearing bit 3 (0-based index from right)?
unsigned char reg = 0xFF; reg &= ~(1 << 3); printf("0x%X", reg);
Use bitwise AND with the inverse mask to clear a bit.
Bit 3 corresponds to the value 8 (1 << 3). Clearing it from 0xFF (255) results in 255 - 8 = 247, which is 0xF7 in hex.
What is the value of reg after clearing bit 0 of reg = 0x1234?
unsigned short reg = 0x1234; reg &= ~(1 << 0); printf("0x%X", reg);
Clearing bit 0 means setting the least significant bit to 0.
Bit 0 of 0x1234 is already 0, so clearing it does not change the value.
Consider this code snippet intended to clear bit 5 of reg:
unsigned int reg = 0xFF; reg = reg & (1 << 5);
What is the problem?
Check what the bitwise AND with (1 << 5) does.
The expression reg & (1 << 5) keeps only bit 5 and clears all others. Assigning this back to reg removes all bits except bit 5, which is the opposite of clearing bit 5.
Which option contains a syntax error when trying to clear bit 2 of reg?
unsigned char reg = 0xFF;Look at operator precedence and parentheses.
Option C lacks parentheses around 1 << 2, so the bitwise NOT applies only to 1, then shifted left, causing unexpected behavior and a syntax warning or logic error.
Given unsigned int reg = 0xFFFF;, how many bits are cleared after executing reg &= ~(0xF << 4);?
Consider how many bits are set in 0xF and where they are shifted.
0xF is 4 bits set (1111). Shifting left by 4 moves these 4 bits to positions 4 to 7. Clearing these bits clears exactly 4 bits.