XOR for toggling bits in Embedded C - Time & Space Complexity
We want to see how the time to toggle bits changes as the number of bits grows.
How does the work needed grow when we toggle more bits?
Analyze the time complexity of the following code snippet.
void toggle_bits(unsigned int *num, unsigned int mask) {
*num = *num ^ mask;
}
This code toggles bits in a number using XOR with a mask.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One XOR operation on the whole number.
- How many times: Exactly once per call, no loops or recursion.
The time to toggle bits stays the same no matter how many bits we have.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 1 XOR operation |
| 32 | 1 XOR operation |
| 64 | 1 XOR operation |
Pattern observation: The operation count does not increase with input size.
Time Complexity: O(1)
This means toggling bits takes the same amount of time no matter how many bits are involved.
[X] Wrong: "Toggling more bits means more time because each bit changes separately."
[OK] Correct: XOR toggles all bits in one step using a single operation, so time does not grow with bit count.
Understanding how bitwise operations work efficiently shows you know how low-level code runs fast, a useful skill in many embedded and systems tasks.
"What if we toggled bits one by one in a loop instead of using XOR? How would the time complexity change?"
Practice
^) do when used to toggle bits in embedded C?Solution
Step 1: Understand XOR operation basics
XOR flips bits where the mask bit is 1 and keeps bits unchanged where mask bit is 0.Step 2: Apply to toggling bits
Using XOR with a mask toggles only the bits set in the mask, leaving others unchanged.Final Answer:
It flips bits where the mask has 1s and leaves others unchanged. -> Option BQuick Check:
XOR toggles bits = D [OK]
- Thinking XOR sets bits to 1 always
- Confusing XOR with AND or OR
- Assuming XOR shifts bits
flags using XOR in embedded C?Solution
Step 1: Identify toggle operation syntax
To toggle a bit, XOR (^) with a mask having 1 at that bit position is used.Step 2: Check each option
flags = flags ^ (1 << 2); uses XOR with (1 << 2) which correctly toggles bit 2. Others set, clear, or shift bits incorrectly.Final Answer:
flags = flags ^ (1 << 2); -> Option AQuick Check:
Toggle bit syntax = B [OK]
- Using OR instead of XOR to toggle
- Using AND with negated mask to clear bits
- Using shift operator alone
unsigned char flags = 0b00001100;
flags = flags ^ 0b00000110;
printf("0x%02X", flags);Solution
Step 1: Convert initial flags and mask to binary
flags = 00001100 (0x0C), mask = 00000110 (0x06)Step 2: Apply XOR operation bitwise
00001100 ^ 00000110 = 00001010 (0x0A)Final Answer:
0x0A -> Option CQuick Check:
0x0C ^ 0x06 = 0x0A [OK]
- Adding values instead of XORing
- Misreading binary bits
- Confusing hex output format
status. What is the error?unsigned int status = 0x01; status = status & (1 << 1);
Solution
Step 1: Analyze the operator used
The code uses AND (&) with mask, which clears bits not set in mask instead of toggling.Step 2: Identify correct operator for toggling
XOR (^) is the correct operator to toggle bits, not AND (&).Final Answer:
Using AND instead of XOR will clear bits instead of toggling. -> Option DQuick Check:
Toggle needs XOR, not AND [OK]
- Confusing AND with XOR for toggling
- Incorrect bit shift amount
- Wrong data type assumption
reg initially set to 0b10101010. You want to toggle bits 1, 3, and 5 simultaneously using XOR. Which mask should you use and what will be the final value of reg after toggling?Solution
Step 1: Create mask for bits 1, 3, and 5
Bits are zero-indexed from right. Bit 1 = 0b00000010, bit 3 = 0b00001000, bit 5 = 0b00100000. OR them: 0b00101010.Step 2: XOR reg with mask
reg = 0b10101010 ^ 0b00101010 = 0b10000000.Final Answer:
Mask: 0b00101010; Final reg: 0b10000000 -> Option AQuick Check:
Toggle bits 1,3,5 with mask 0x2A = 0x80 [OK]
- Wrong mask bits set
- Miscounting bit positions
- Incorrect XOR result calculation
Ready to prove it?
Pass the test and this pattern moves unknown
