Discover how a tiny operation can save you from hours of tedious checking!
Why Checking if a bit is set in Embedded C? - Purpose & Use Cases
Imagine you have a control panel with many switches, and you want to know if a specific switch is ON or OFF. Doing this by looking at each switch one by one is slow and tiring.
Manually checking each switch means you must look at every single one, which takes time and can cause mistakes if you lose track or misread a switch.
By using bit checking, you can quickly and reliably check if a specific switch (bit) is ON or OFF with a simple operation, saving time and avoiding errors.
if ((value & (1 << 3)) != 0) { // bit 3 is set }
bool isSet = (value & (1 << 3)) != 0;
This lets you efficiently control and monitor hardware states with minimal code and maximum reliability.
Checking if a sensor is active by reading a specific bit in a status register on a microcontroller.
Manually checking bits is slow and error-prone.
Bit checking uses simple operations to test specific bits quickly.
This technique is essential for embedded systems and hardware control.
Practice
(num & (1 << 3)) != 0 check in embedded C?Solution
Step 1: Understand the bit shift operation
The expression1 << 3shifts 1 left by 3 bits, creating a mask with only the 4th bit set (value 8).Step 2: Apply bitwise AND to check the bit
The bitwise ANDnum & (1 << 3)isolates the 4th bit ofnum. If this result is not zero, the bit is set.Final Answer:
Checks if the 4th bit (bit 3) of num is set to 1 -> Option BQuick Check:
Bitwise AND with mask = non-zero means bit set [OK]
- Confusing bit position with bit value
- Forgetting bits start at 0
- Using OR instead of AND to check bits
x is set in embedded C?Solution
Step 1: Identify the mask for bit 1
The mask for the 2nd bit is1 << 1, which equals 2.Step 2: Use bitwise AND to check the bit
Usingx & (1 << 1)checks if bit 1 is set. If the result is non-zero, the bit is set.Final Answer:
if (x & (1 << 1)) { /* bit is set */ } -> Option DQuick Check:
Bitwise AND with shifted 1 checks bit [OK]
- Using bitwise OR (|) instead of AND (&)
- Using left shift on variable instead of mask
- Checking wrong bit position
unsigned int num = 13; // binary 1101
if (num & (1 << 2))
printf("Bit 2 is set\n");
else
printf("Bit 2 is not set\n");Solution
Step 1: Convert num to binary and identify bit 2
Number 13 in binary is 1101. Bits are numbered from 0 (rightmost). Bit 2 is the third bit from right, which is 1.Step 2: Check bit 2 using bitwise AND
The expressionnum & (1 << 2)equals13 & 4which is 4 (non-zero), so the condition is true.Final Answer:
Bit 2 is set -> Option CQuick Check:
Bit 2 in 1101 is 1, so output is "Bit 2 is set" [OK]
- Miscounting bit positions starting at 1 instead of 0
- Assuming bit 2 is zero without checking
- Confusing bitwise AND with OR
flags is set. What is the error?
unsigned int flags = 0x20;
if (flags & 5 << 1) {
// bit is set
}Solution
Step 1: Analyze operator precedence in expression
The expressionflags & 5 << 1is evaluated asflags & (5 << 1)due to operator precedence (<< higher than &).Step 2: Understand the intended mask and fix
The intended mask is1 << 5to check bit 5. The current expression does not create the correct mask.Final Answer:
Incorrect operator precedence causes wrong mask -> Option AQuick Check:
Use parentheses: flags & (1 << 5) to fix [OK]
- Not using parentheses causing wrong evaluation
- Shifting wrong constant (5 instead of 1)
- Confusing bit positions and mask values
status. Write a condition to check if either bit 1 or bit 3 is set. Which of the following is correct?Solution
Step 1: Create mask for bits 1 and 3
Use bitwise OR to combine masks:(1 << 1) | (1 << 3)creates a mask with bits 1 and 3 set.Step 2: Use bitwise AND to check if either bit is set
Check ifstatus & maskis non-zero to confirm if either bit is set.Final Answer:
if (status & ((1 << 1) | (1 << 3))) { /* bit 1 or 3 set */ } -> Option AQuick Check:
Combine masks with |, check with & for any bit set [OK]
- Using && instead of | to combine masks
- Using + instead of | causing wrong mask
- Using bitwise OR on variable instead of mask
Ready to prove it?
Pass the test and this pattern moves unknown
