0
0
Embedded Cprogramming~20 mins

XOR for toggling bits in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XOR Bit Toggling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A170
B250
C90
D85
Attempts:
2 left
💡 Hint
XOR flips bits where the mask has 1s.
Predict Output
intermediate
2: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;
}
A240
B0
C255
D15
Attempts:
2 left
💡 Hint
XOR with 0xFF flips all bits.
🔧 Debug
advanced
2: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;
}
AThe shift operator << is not valid in C
BVariable z is not initialized properly
CXOR operator ^ is used instead of OR operator |
DOperator precedence causes incorrect mask; XOR uses wrong bit mask
Attempts:
2 left
💡 Hint
Check how the shift and XOR operators combine.
📝 Syntax
advanced
1: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.
Ax ^= 1 << 3;
Bx ^= (1 << 3)
Cx = x ^ (1 << 3);
D;3 << 1 =^ x
Attempts:
2 left
💡 Hint
Check for missing semicolons.
🚀 Application
expert
3: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;
}
A4
B2
C6
D8
Attempts:
2 left
💡 Hint
Count the number of 1s in the mask.