Sometimes you want to turn off a single switch inside a group of switches without changing the others. Clearing a specific bit in a register helps you do that in a computer chip.
Clearing a specific bit in a register in Embedded C
register &= ~(1U << bit_position);register is the variable holding the bits.
bit_position is the number of the bit you want to clear, starting from 0 for the least significant bit.
PORTA &= ~(1U << 3);
status_register.status_register &= ~(1U << 0);
control_reg.control_reg &= ~(1U << 7);
This program starts with all bits set to 1 in an 8-bit register. It clears bit number 4 and prints the register value before and after clearing.
#include <stdio.h> int main() { unsigned char reg = 0b11111111; // all bits set to 1 int bit_to_clear = 4; printf("Before clearing bit %d: 0x%02X\n", bit_to_clear, reg); reg &= ~(1U << bit_to_clear); // clear bit 4 printf("After clearing bit %d: 0x%02X\n", bit_to_clear, reg); return 0; }
Bits are counted from 0 starting at the right (least significant bit).
Using ~(1U << bit_position) creates a mask with all bits set to 1 except the bit you want to clear.
Make sure the register variable is the correct size to hold the bits you want to manipulate.
Clearing a bit means setting it to 0 without changing other bits.
Use register &= ~(1U << bit_position); to clear a specific bit.
This technique is useful for controlling hardware features bit by bit.
Practice
register &= ~(1 << 3); do in embedded C?Solution
Step 1: Understand bit shifting and negation
The expression1 << 3shifts 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 BQuick Check:
Clear bit 3 = register &= ~(1 << 3) [OK]
- Confusing clearing with setting a bit
- Using OR instead of AND
- Not applying bitwise NOT before AND
reg?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 AQuick Check:
Clear bit 5 = reg &= ~(1 << 5) [OK]
- Using OR (|) instead of AND (&)
- Forgetting to negate the mask
- Assigning instead of using compound assignment
unsigned char reg = 0b11111111; and the operation reg &= ~(1 << 2);, what is the binary value of reg after execution?Solution
Step 1: Identify the bit to clear
Bit 2 means the third bit from the right (0-based index). The mask is1 << 2 = 0b00000100.Step 2: Apply clearing operation
Negate mask:~0b00000100 = 0b11111011. AND withreg:0b11111111 & 0b11111011 = 0b11111011.Final Answer:
0b11111011 -> Option AQuick Check:
Clear bit 2 from 0xFF = 0xFB (binary 11111011) [OK]
- Clearing wrong bit index
- Not negating the mask
- Confusing bit positions (1-based vs 0-based)
reg?
reg = reg & (1 << 4);
Solution
Step 1: Analyze the operation performed
The expressionreg & (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 CQuick Check:
AND with mask keeps only that bit [OK]
- Using AND with mask instead of negated mask
- Confusing clearing with masking
- Expecting toggling behavior
uint16_t reg = 0xFFFF;. You want to clear bits 0, 4, and 15 simultaneously. Which code correctly does this?Solution
Step 1: Create a mask for bits to clear
Bits 0, 4, and 15 correspond to masks1<<0,1<<4, and1<<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 withregclears those bits.Final Answer:
reg &= ~( (1 << 0) | (1 << 4) | (1 << 15) ); -> Option DQuick Check:
Clear multiple bits by AND with negated OR mask [OK]
- Using OR instead of AND to clear bits
- Not negating the combined mask
- Clearing only one bit instead of all three
Ready to prove it?
Pass the test and this pattern moves unknown
