Challenge - 5 Problems
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this bitwise NOT operation?
Consider the following C code snippet. What will be printed when it runs?
Embedded C
#include <stdio.h> int main() { unsigned char x = 0b10101010; unsigned char y = ~x; printf("%u\n", y); return 0; }
Attempts:
2 left
💡 Hint
Remember that ~ flips all bits, and unsigned char is 8 bits.
✗ Incorrect
The original value x is 0b10101010 (decimal 170). The bitwise NOT (~) flips all bits, so y becomes 0b01010101, which is decimal 85.
❓ Predict Output
intermediate2:00remaining
What is the output when applying NOT to a signed integer?
Look at this C code. What will it print?
Embedded C
#include <stdio.h> int main() { int x = 0; int y = ~x; printf("%d\n", y); return 0; }
Attempts:
2 left
💡 Hint
Think about how ~ works on zero in two's complement.
✗ Incorrect
For a signed int, ~0 flips all bits from 000...0 to 111...1, which is -1 in two's complement.
❓ Predict Output
advanced2:00remaining
What is the output of this NOT operation on a 16-bit unsigned short?
Given this code, what will be printed?
Embedded C
#include <stdio.h> #include <stdint.h> int main() { uint16_t x = 0x00FF; uint16_t y = ~x; printf("0x%04X\n", y); return 0; }
Attempts:
2 left
💡 Hint
NOT flips all bits in the 16-bit number.
✗ Incorrect
0x00FF in binary is 0000000011111111. Flipping all bits gives 1111111100000000, which is 0xFF00.
❓ Predict Output
advanced2:00remaining
What is the output of this NOT operation on a signed char with value -1?
Analyze this code and determine the output.
Embedded C
#include <stdio.h> int main() { signed char x = -1; signed char y = ~x; printf("%d\n", y); return 0; }
Attempts:
2 left
💡 Hint
Remember that -1 in two's complement is all bits set to 1.
✗ Incorrect
For signed char, -1 is 0xFF (all bits 1). ~x flips all bits to 0x00, which is 0.
🧠 Conceptual
expert3:00remaining
Why does using NOT (~) not always simply invert bits in embedded C?
Which statement best explains why applying the NOT operator (~) in embedded C might not always produce a simple bit inversion effect?
Attempts:
2 left
💡 Hint
Think about how data types and sign affect bitwise operations.
✗ Incorrect
The NOT operator flips all bits of the value, but the number of bits depends on the data type size. For signed types, sign extension and two's complement representation affect the final value, so the result might not look like a simple inversion.