Given a register REG initially set to 0x0F, what is the value of REG after setting bit 5?
unsigned char REG = 0x0F; REG |= (1 << 5); printf("0x%02X", REG);
Remember that 1 << 5 sets bit 5 (counting from 0).
Starting with 0x0F (00001111), setting bit 5 (bit mask 00100000) results in 00101111 which is 0x2F.
Given REG = 0xFF, what is the value of REG after clearing bit 3?
unsigned char REG = 0xFF; REG &= ~(1 << 3); printf("0x%02X", REG);
Clearing a bit means setting it to 0 using AND with the inverse mask.
Bit 3 mask is 00001000 (0x08). Inverting gives 11110111 (0xF7). AND with 0xFF clears bit 3, resulting in 0xF7.
Given REG = 0x55 (binary 01010101), what is the value of REG after toggling bits 0, 2, and 4?
unsigned char REG = 0x55; REG ^= (1 << 0) | (1 << 2) | (1 << 4); printf("0x%02X", REG);
Toggling flips bits: 1 becomes 0, 0 becomes 1.
Bits 0,2,4 mask is 00010101 (0x15). XOR with 0x55 (01010101) flips those bits resulting in 01000000 (0x40).
Given REG = 0xA0, what is the output of the following code?
unsigned char REG = 0xA0; if (REG & (1 << 7)) printf("Set"); else printf("Clear");
Bit 7 corresponds to the highest bit in an 8-bit register.
0xA0 in binary is 10100000. Bit 7 is set (1), so the condition is true and "Set" is printed.
Given REG = 0x3C, and the operation REG = (REG << 1) | (REG >> 7); (a rotate left by 1 bit), how many bits are set (1) in REG after this operation?
unsigned char REG = 0x3C; REG = (REG << 1) | (REG >> 7); // Count bits set in REG
Rotate left shifts bits left and wraps the leftmost bit to the rightmost position.
0x3C is 00111100. Rotating left by 1 bit gives 01111000 (0x78). Number of set bits is 4.