0
0
Embedded Cprogramming~10 mins

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

Choose your learning style9 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.