We set a specific bit in a register to turn on a feature or control hardware without changing other settings.
Setting a specific bit in a register in Embedded C
REGISTER |= (1 << BIT_POSITION);REGISTER is the name of the hardware register.
BIT_POSITION is the zero-based position of the bit to set (0 for least significant bit).
PORTA |= (1 << 3);
CONTROL_REG |= (1 << 0);
STATUS |= (1 << 7);
This program starts with all bits off in registerA. It sets bit 2 to 1 using the bitwise OR and shift. Then it prints the register bits from left to right.
#include <stdio.h> int main() { unsigned char registerA = 0b00000000; // all bits off int bit_to_set = 2; // we want to set bit 2 // Set bit 2 registerA |= (1 << bit_to_set); printf("Register after setting bit %d: 0b", bit_to_set); for (int i = 7; i >= 0; i--) { printf("%d", (registerA >> i) & 1); } printf("\n"); return 0; }
Using |= keeps other bits unchanged while setting the chosen bit.
Bit positions start at 0 from the right (least significant bit).
Make sure the register variable is wide enough to hold the bits you want to set.
Use REGISTER |= (1 << BIT_POSITION); to set a specific bit.
This changes only the chosen bit without affecting others.
Bit positions count from 0 at the rightmost bit.
Practice
REGISTER |= (1 << 3); do in embedded C?Solution
Step 1: Understand bit shifting
The expression1 << 3shifts the number 1 three places to the left, resulting in binary 00001000, which targets bit position 3.Step 2: Apply bitwise OR to set bit
Using|=with this mask sets bit 3 to 1 in REGISTER without changing other bits.Final Answer:
Sets the 4th bit (bit position 3) of REGISTER to 1 without changing other bits -> Option BQuick Check:
Bit shift + OR sets specific bit = A [OK]
- Confusing bit positions starting from 1 instead of 0
- Using & instead of | to set bits
- Thinking it clears bits instead of setting
PORTA?Solution
Step 1: Identify correct operator to set bit
To set a bit without affecting others, use bitwise OR assignment|=with a mask.Step 2: Check the bit mask and syntax
1 << 5creates a mask for bit 5. Combining with|=sets that bit.Final Answer:
PORTA |= (1 << 5); -> Option DQuick Check:
Use |= with shifted 1 to set bit [OK]
- Using = instead of |= overwrites all bits
- Using &= ~ to clear bits, not set
- Using ^= toggles bits instead of setting
unsigned char REG = 0x12; (binary 00010010), what is the value of REG after executing REG |= (1 << 4);?Solution
Step 1: Convert initial REG value to binary
0x12 in binary is 00010010. Bit positions (0 to 7) from right to left.Step 2: Calculate mask and apply OR
1 << 4is 00010000 (bit 4 set). OR with 00010010 results in 00110010 (0x32).Final Answer:
0x32 -> Option AQuick Check:
0x12 | 0x10 = 0x32 [OK]
- Miscounting bit positions starting from 1
- Confusing hex and decimal values
- Using &= or ^= instead of |=
REG:
unsigned char REG = 0x00; REG = REG & (1 << 2);
Solution
Step 1: Analyze bitwise AND usage
The code uses&with mask(1 << 2), which clears all bits except bit 2.Step 2: Understand effect on REG
Since REG starts at 0, AND with mask results in 0, not setting bit 2. To set bit 2, OR|=should be used.Final Answer:
Using & instead of | clears all bits except bit 2 -> Option AQuick Check:
Use |= to set bits, & clears bits [OK]
- Confusing & and | operators
- Assuming &= sets bits
- Ignoring initial value of REG
REG16 initially set to 0x0000. Write the correct statement to set bits 0, 7, and 15 simultaneously without affecting other bits.Solution
Step 1: Create mask for bits 0, 7, and 15
Use bitwise OR to combine masks:(1 << 0) | (1 << 7) | (1 << 15)sets bits 0, 7, and 15.Step 2: Use |= to set bits without clearing others
Using|=applies the mask to REG16, setting those bits while preserving others.Final Answer:
REG16 |= (1 << 0) | (1 << 7) | (1 << 15); -> Option CQuick Check:
Combine masks with | and set with |= [OK]
- Using & instead of | to combine masks
- Using = overwrites all bits, losing previous data
- Using & instead of | in assignment
Ready to prove it?
Pass the test and this pattern moves unknown
