Bird
Raised Fist0
Embedded Cprogramming~10 mins

Checking if a bit is set in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Checking if a bit is set
Start with number
↓
Shift 1 << bit_position
↓
AND operation: number & mask
↓
Check if result != 0
↓
Bit is set
We create a mask by shifting 1 to the bit position, then use AND to check if that bit is set in the number.
Execution Sample
Embedded C
int number = 13; // binary 1101
int bit = 2;
int mask = 1 << bit;
int result = number & mask;
if (result != 0) {
  // bit is set
}
This code checks if the 3rd bit (bit 2) in number 13 is set.
Execution Table
StepVariableValueOperationResult/Output
1number13 (binary 1101)Initialize number13
2bit2Initialize bit position2
3mask1 << 2Shift 1 left by 2 bits4 (binary 0100)
4resultnumber & maskAND 1101 & 01004 (non-zero)
5if conditionresult != 0Check if result is not zeroTrue - bit is set
6OutputBit is setPrint or act on bit setBit is set
💡 Finished checking bit 2 in number 13; condition true because bit is set.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
number13131313
bit2222
maskundefined444
resultundefinedundefined44
Key Moments - 3 Insights
Why do we use '1 << bit' to create the mask?
Because shifting 1 left by 'bit' positions creates a number with only that bit set, which we use to check the bit in 'number' (see execution_table step 3).
Why does 'number & mask' tell us if the bit is set?
Because ANDing keeps only the bits that are set in both numbers. If the bit is set in 'number', the result won't be zero (see execution_table step 4).
What if the result of 'number & mask' is zero?
It means the bit is not set in 'number', so the condition 'result != 0' is false and we know the bit is off (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'mask'?
A2 (binary 0010)
B8 (binary 1000)
C4 (binary 0100)
D1 (binary 0001)
💡 Hint
Check the 'Value' column for 'mask' at step 3 in execution_table.
At which step does the program determine if the bit is set or not?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look for the step where the if condition 'result != 0' is evaluated in execution_table.
If 'number' was 8 (binary 1000) and 'bit' was 2, what would 'result' be at step 4?
A0 (bit not set)
B4 (bit set)
C8 (bit set)
D2 (bit not set)
💡 Hint
Use the variable_tracker logic and AND operation: 8 & 4 = ?
Concept Snapshot
Check if a bit is set:
- Create mask: 1 << bit_position
- AND mask with number: number & mask
- If result != 0, bit is set
- Else, bit is not set
Simple and fast bit test.
Full Transcript
This visual trace shows how to check if a specific bit is set in a number using embedded C. We start with a number and a bit position. We create a mask by shifting 1 left by the bit position. Then we AND the number with the mask. If the result is not zero, the bit is set; otherwise, it is not. The execution table walks through each step with variable values. The variable tracker shows how variables change. Key moments clarify common confusions about shifting and AND operations. The quiz tests understanding of mask creation, condition checking, and bit testing logic.

Practice

(1/5)
1. What does the expression (num & (1 << 3)) != 0 check in embedded C?
easy
A. If the 1st bit (bit 0) of num is set to 1
B. If the 4th bit (bit 3) of num is set to 1
C. If num is equal to 8
D. If num is an even number

Solution

  1. Step 1: Understand the bit shift operation

    The expression 1 << 3 shifts 1 left by 3 bits, creating a mask with only the 4th bit set (value 8).
  2. Step 2: Apply bitwise AND to check the bit

    The bitwise AND num & (1 << 3) isolates the 4th bit of num. If this result is not zero, the bit is set.
  3. Final Answer:

    Checks if the 4th bit (bit 3) of num is set to 1 -> Option B
  4. Quick Check:

    Bitwise AND with mask = non-zero means bit set [OK]
Hint: Use (1 << bit) & num to check bit set quickly [OK]
Common Mistakes:
  • Confusing bit position with bit value
  • Forgetting bits start at 0
  • Using OR instead of AND to check bits
2. Which of the following is the correct syntax to check if the 2nd bit (bit 1) of variable x is set in embedded C?
easy
A. if (x & 1) { /* bit is set */ }
B. if (x | (1 << 1)) { /* bit is set */ }
C. if (x << 1) { /* bit is set */ }
D. if (x & (1 << 1)) { /* bit is set */ }

Solution

  1. Step 1: Identify the mask for bit 1

    The mask for the 2nd bit is 1 << 1, which equals 2.
  2. Step 2: Use bitwise AND to check the bit

    Using x & (1 << 1) checks if bit 1 is set. If the result is non-zero, the bit is set.
  3. Final Answer:

    if (x & (1 << 1)) { /* bit is set */ } -> Option D
  4. Quick Check:

    Bitwise AND with shifted 1 checks bit [OK]
Hint: Use & with (1 << bit) to check bits, not | or << alone [OK]
Common Mistakes:
  • Using bitwise OR (|) instead of AND (&)
  • Using left shift on variable instead of mask
  • Checking wrong bit position
3. What is the output of the following code snippet?
unsigned int num = 13; // binary 1101
if (num & (1 << 2))
    printf("Bit 2 is set\n");
else
    printf("Bit 2 is not set\n");
medium
A. Compilation error
B. Bit 2 is not set
C. Bit 2 is set
D. No output

Solution

  1. 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.
  2. Step 2: Check bit 2 using bitwise AND

    The expression num & (1 << 2) equals 13 & 4 which is 4 (non-zero), so the condition is true.
  3. Final Answer:

    Bit 2 is set -> Option C
  4. Quick Check:

    Bit 2 in 1101 is 1, so output is "Bit 2 is set" [OK]
Hint: Convert number to binary, check bit position with mask [OK]
Common Mistakes:
  • Miscounting bit positions starting at 1 instead of 0
  • Assuming bit 2 is zero without checking
  • Confusing bitwise AND with OR
4. The following code is intended to check if bit 5 of flags is set. What is the error?
unsigned int flags = 0x20;
if (flags & 5 << 1) {
    // bit is set
}
medium
A. Incorrect operator precedence causes wrong mask
B. Bit shift should be on variable, not constant
C. Missing parentheses around bit shift
D. Using bitwise OR instead of AND

Solution

  1. Step 1: Analyze operator precedence in expression

    The expression flags & 5 << 1 is evaluated as flags & (5 << 1) due to operator precedence (<< higher than &).
  2. Step 2: Understand the intended mask and fix

    The intended mask is 1 << 5 to check bit 5. The current expression does not create the correct mask.
  3. Final Answer:

    Incorrect operator precedence causes wrong mask -> Option A
  4. Quick Check:

    Use parentheses: flags & (1 << 5) to fix [OK]
Hint: Always use parentheses around (1 << bit) mask [OK]
Common Mistakes:
  • Not using parentheses causing wrong evaluation
  • Shifting wrong constant (5 instead of 1)
  • Confusing bit positions and mask values
5. You have an 8-bit status register status. Write a condition to check if either bit 1 or bit 3 is set. Which of the following is correct?
hard
A. if (status & ((1 << 1) | (1 << 3))) { /* bit 1 or 3 set */ }
B. if ((status & (1 << 1)) && (status & (1 << 3))) { /* bit 1 or 3 set */ }
C. if (status | (1 << 1) | (1 << 3)) { /* bit 1 or 3 set */ }
D. if (status & (1 << (1 + 3))) { /* bit 1 or 3 set */ }

Solution

  1. 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.
  2. Step 2: Use bitwise AND to check if either bit is set

    Check if status & mask is non-zero to confirm if either bit is set.
  3. Final Answer:

    if (status & ((1 << 1) | (1 << 3))) { /* bit 1 or 3 set */ } -> Option A
  4. Quick Check:

    Combine masks with |, check with & for any bit set [OK]
Hint: Combine bit masks with | inside parentheses before & [OK]
Common Mistakes:
  • Using && instead of | to combine masks
  • Using + instead of | causing wrong mask
  • Using bitwise OR on variable instead of mask
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard