0
0
Embedded Cprogramming~20 mins

Register bit manipulation patterns in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Register Bit Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this register bit set operation?

Given a register REG initially set to 0x0F, what is the value of REG after setting bit 5?

Embedded C
unsigned char REG = 0x0F;
REG |= (1 << 5);
printf("0x%02X", REG);
A0x2F
B0x1F
C0x3F
D0x0F
Attempts:
2 left
💡 Hint

Remember that 1 << 5 sets bit 5 (counting from 0).

Predict Output
intermediate
2:00remaining
What is the output after clearing a bit in a register?

Given REG = 0xFF, what is the value of REG after clearing bit 3?

Embedded C
unsigned char REG = 0xFF;
REG &= ~(1 << 3);
printf("0x%02X", REG);
A0xEF
B0xFB
C0x7F
D0xF7
Attempts:
2 left
💡 Hint

Clearing a bit means setting it to 0 using AND with the inverse mask.

Predict Output
advanced
2:00remaining
What is the output after toggling bits in a register?

Given REG = 0x55 (binary 01010101), what is the value of REG after toggling bits 0, 2, and 4?

Embedded C
unsigned char REG = 0x55;
REG ^= (1 << 0) | (1 << 2) | (1 << 4);
printf("0x%02X", REG);
A0x40
B0x15
C0x55
D0x75
Attempts:
2 left
💡 Hint

Toggling flips bits: 1 becomes 0, 0 becomes 1.

Predict Output
advanced
2:00remaining
What is the output after checking a bit in a register?

Given REG = 0xA0, what is the output of the following code?

Embedded C
unsigned char REG = 0xA0;
if (REG & (1 << 7))
    printf("Set");
else
    printf("Clear");
AClear
BSet
CCompile error
DNo output
Attempts:
2 left
💡 Hint

Bit 7 corresponds to the highest bit in an 8-bit register.

🧠 Conceptual
expert
3:00remaining
How many bits are set in this register after the operation?

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?

Embedded C
unsigned char REG = 0x3C;
REG = (REG << 1) | (REG >> 7);
// Count bits set in REG
A3
B5
C4
D6
Attempts:
2 left
💡 Hint

Rotate left shifts bits left and wraps the leftmost bit to the rightmost position.