0
0
Embedded Cprogramming~20 mins

Clearing a specific bit in a register in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after clearing bit 3?

Given the register value 0b11111111, what is the value of reg after clearing bit 3 (0-based index from right)?

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

Use bitwise AND with the inverse mask to clear a bit.

Predict Output
intermediate
2:00remaining
Result of clearing bit 0 in a 16-bit register

What is the value of reg after clearing bit 0 of reg = 0x1234?

Embedded C
unsigned short reg = 0x1234;
reg &= ~(1 << 0);
printf("0x%X", reg);
A0x1234
B0x1235
C0x1236
D4321x0
Attempts:
2 left
💡 Hint

Clearing bit 0 means setting the least significant bit to 0.

🔧 Debug
advanced
2:00remaining
Why does this code fail to clear bit 5?

Consider this code snippet intended to clear bit 5 of reg:

unsigned int reg = 0xFF;
reg = reg & (1 << 5);

What is the problem?

AIt causes a syntax error due to missing parentheses
BIt sets all bits except bit 5 to zero, instead of clearing bit 5
CIt clears all bits including bit 5
DIt does nothing because bit 5 is already zero
Attempts:
2 left
💡 Hint

Check what the bitwise AND with (1 << 5) does.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in clearing a bit

Which option contains a syntax error when trying to clear bit 2 of reg?

Embedded C
unsigned char reg = 0xFF;
A;)2 << 1(~ & ger = ger
Breg = reg & ~(1 << 2);
Creg &= ~1 << 2;
Dreg &= ~(1 << 2);
Attempts:
2 left
💡 Hint

Look at operator precedence and parentheses.

🚀 Application
expert
3:00remaining
How many bits are cleared after this operation?

Given unsigned int reg = 0xFFFF;, how many bits are cleared after executing reg &= ~(0xF << 4);?

A4 bits
B8 bits
C16 bits
D0 bits
Attempts:
2 left
💡 Hint

Consider how many bits are set in 0xF and where they are shifted.