Challenge - 5 Problems
XOR Bit Toggling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of toggling bits using XOR?
Consider the following C code that toggles bits of an 8-bit variable using XOR. What is the output printed?
Embedded C
#include <stdio.h> int main() { unsigned char x = 0b10101010; // 170 in decimal x = x ^ 0b11110000; printf("%u\n", x); return 0; }
Attempts:
2 left
💡 Hint
XOR flips bits where the mask has 1s.
✗ Incorrect
The original value is 0b10101010 (170). XOR with 0b11110000 flips the top 4 bits: 1010 ^ 1111 = 0101, bottom 4 bits stay the same. Result is 0b01011010 which is 90 in decimal.
❓ Predict Output
intermediate2:00remaining
What value does the variable hold after toggling?
What is the value of variable y after this code runs?
Embedded C
#include <stdio.h> int main() { unsigned char y = 0x0F; // 15 decimal y ^= 0xFF; printf("%u\n", y); return 0; }
Attempts:
2 left
💡 Hint
XOR with 0xFF flips all bits.
✗ Incorrect
0x0F is 00001111 in binary. XOR with 0xFF (11111111) flips all bits, resulting in 11110000 which is 240 decimal.
🔧 Debug
advanced2:30remaining
Why does this XOR toggle code not work as expected?
This code is intended to toggle the 3rd bit (bit 2) of variable z. What is the error causing it to fail?
Embedded C
#include <stdio.h> int main() { unsigned char z = 0b00000100; z = z ^ 2 << 2; printf("%u\n", z); return 0; }
Attempts:
2 left
💡 Hint
Check how the shift and XOR operators combine.
✗ Incorrect
The expression '2 << 2' shifts 2 (binary 10) left by 2 bits, resulting in 8 (binary 1000). The intended mask to toggle bit 2 is 1 << 2 (binary 100). Using 2 << 2 creates wrong mask, so toggling affects wrong bit.
📝 Syntax
advanced1:30remaining
Which option causes a syntax error in this XOR toggle snippet?
Identify which code snippet will cause a syntax error when toggling bits using XOR.
Attempts:
2 left
💡 Hint
Check for missing semicolons.
✗ Incorrect
Option B is missing a semicolon at the end, causing a syntax error. All others have proper syntax.
🚀 Application
expert3:00remaining
How many bits are toggled after this code runs?
Given the code below, how many bits in variable a are toggled after execution?
Embedded C
#include <stdio.h> int main() { unsigned char a = 0b11001100; unsigned char mask = 0b10101010; a ^= mask; printf("%u\n", a); return 0; }
Attempts:
2 left
💡 Hint
Count the number of 1s in the mask.
✗ Incorrect
The mask 0b10101010 has four 1 bits. XOR toggles bits where mask bits are 1, so 4 bits are toggled.