📖 Scenario: You are working with a microcontroller and need to control hardware by changing bits in a register. Each bit in the register controls a different feature. You want to turn off (clear) a specific feature by clearing its bit.
🎯 Goal: Learn how to clear a specific bit in a register using bitwise operations in C.
📋 What You'll Learn
Create a variable to represent the register with a specific initial value.
Create a variable to hold the bit position you want to clear.
Use bitwise operations to clear the bit at the given position in the register.
Print the register value before and after clearing the bit.
💡 Why This Matters
🌍 Real World
Microcontrollers and embedded systems often require controlling hardware features by setting or clearing bits in registers.
💼 Career
Understanding bitwise operations is essential for embedded software engineers, firmware developers, and anyone working close to hardware.
Progress0 / 4 steps
1
Create the register variable
Create an unsigned int variable called register_value and set it to 0b11111111 (255 in decimal).
Embedded C
Hint
Use binary notation 0b11111111 to set all bits to 1.
2
Create the bit position variable
Create an unsigned int variable called bit_position and set it to 3 to represent the bit you want to clear.
Embedded C
Hint
The bit positions start from 0 for the rightmost bit.
3
Clear the specific bit in the register
Use bitwise operations to clear the bit at bit_position in register_value. Update register_value with the new value.
Embedded C
Hint
Use 1 << bit_position to create a mask, then invert it with ~ and use &= to clear the bit.
4
Print the register value before and after clearing the bit
Print the value of register_value before and after clearing the bit using printf. Use %#010b to show the binary format with leading zeros.
Embedded C
Hint
Use printf twice: once before clearing and once after clearing the bit. Since C does not support %b format specifier, create a helper function to print binary.
Practice
(1/5)
1. What does the expression register &= ~(1 << 3); do in embedded C?
easy
A. Sets the 3rd bit of the register to 1
B. Clears the 3rd bit of the register (sets it to 0) without changing other bits
C. Toggles the 3rd bit of the register
D. Clears all bits of the register
Solution
Step 1: Understand bit shifting and negation
The expression 1 << 3 shifts 1 to the left by 3 bits, creating a mask with only the 3rd bit set (00001000).
Step 2: Apply bitwise NOT and AND assignment
The bitwise NOT ~(1 << 3) flips all bits, making the 3rd bit 0 and others 1. ANDing this with the register clears only the 3rd bit.
Final Answer:
Clears the 3rd bit of the register (sets it to 0) without changing other bits -> Option B
Quick Check:
Clear bit 3 = register &= ~(1 << 3) [OK]
Hint: Use AND with NOT mask to clear a bit [OK]
Common Mistakes:
Confusing clearing with setting a bit
Using OR instead of AND
Not applying bitwise NOT before AND
2. Which of the following is the correct syntax to clear bit 5 of a variable named reg?
easy
A. reg &= ~(1 << 5);
B. reg |= ~(1 << 5);
C. reg = reg & (1 << 5);
D. reg = reg | (1 << 5);
Solution
Step 1: Identify the operation to clear a bit
Clearing a bit means setting it to 0. This is done by ANDing with the negation of the bit mask.
Step 2: Check each option's operation
reg &= ~(1 << 5); uses &= ~(1 << 5), which correctly clears bit 5. Others either set or incorrectly manipulate bits.
Final Answer:
reg &= ~(1 << 5); -> Option A
Quick Check:
Clear bit 5 = reg &= ~(1 << 5) [OK]
Hint: Clear bit with AND and NOT mask [OK]
Common Mistakes:
Using OR (|) instead of AND (&)
Forgetting to negate the mask
Assigning instead of using compound assignment
3. Given unsigned char reg = 0b11111111; and the operation reg &= ~(1 << 2);, what is the binary value of reg after execution?
medium
A. 0b11111011
B. 0b11111101
C. 0b11110111
D. 0b11111110
Solution
Step 1: Identify the bit to clear
Bit 2 means the third bit from the right (0-based index). The mask is 1 << 2 = 0b00000100.
Step 2: Apply clearing operation
Negate mask: ~0b00000100 = 0b11111011. AND with reg: 0b11111111 & 0b11111011 = 0b11111011.
Final Answer:
0b11111011 -> Option A
Quick Check:
Clear bit 2 from 0xFF = 0xFB (binary 11111011) [OK]
Hint: Clear bit 2 flips that bit to 0 only [OK]
Common Mistakes:
Clearing wrong bit index
Not negating the mask
Confusing bit positions (1-based vs 0-based)
4. What is wrong with this code snippet if the goal is to clear bit 4 of reg?
reg = reg & (1 << 4);
medium
A. It causes a syntax error
B. It correctly clears bit 4
C. It sets all bits except bit 4 to 0, not clearing bit 4
D. It toggles bit 4 instead of clearing
Solution
Step 1: Analyze the operation performed
The expression reg & (1 << 4) keeps only bit 4 and clears all others, effectively zeroing all bits except bit 4.
Step 2: Compare with correct clearing method
To clear bit 4, we must AND with the negation of the mask: reg &= ~(1 << 4);. The given code does not do this.
Final Answer:
It sets all bits except bit 4 to 0, not clearing bit 4 -> Option C
Quick Check:
AND with mask keeps only that bit [OK]
Hint: AND with mask keeps only that bit, not clears it [OK]
Common Mistakes:
Using AND with mask instead of negated mask
Confusing clearing with masking
Expecting toggling behavior
5. You have a 16-bit register uint16_t reg = 0xFFFF;. You want to clear bits 0, 4, and 15 simultaneously. Which code correctly does this?
hard
A. reg |= ( (1 << 0) | (1 << 4) | (1 << 15) );
B. reg &= ( (1 << 0) | (1 << 4) | (1 << 15) );
C. reg |= ~( (1 << 0) | (1 << 4) | (1 << 15) );
D. reg &= ~( (1 << 0) | (1 << 4) | (1 << 15) );
Solution
Step 1: Create a mask for bits to clear
Bits 0, 4, and 15 correspond to masks 1<<0, 1<<4, and 1<<15. OR them to combine: (1<<0) | (1<<4) | (1<<15).
Step 2: Negate mask and AND with register
Negate the combined mask to get zeros at bits 0,4,15 and ones elsewhere. AND with reg clears those bits.