Challenge - 5 Problems
Bitwise NOT Master
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 it print?
C
#include <stdio.h> int main() { unsigned char x = 0b00001111; // 15 in decimal unsigned char y = ~x; printf("%u\n", y); return 0; }
Attempts:
2 left
💡 Hint
Remember that bitwise NOT flips all bits. For an 8-bit unsigned char, 0b00001111 becomes 0b11110000.
✗ Incorrect
The bitwise NOT operator (~) flips every bit. For 0b00001111 (decimal 15), flipping bits gives 0b11110000, which is 240 in decimal.
❓ Predict Output
intermediate2:00remaining
What is the output of bitwise NOT on a signed integer?
What will this C program print?
C
#include <stdio.h> int main() { int x = 0; int y = ~x; printf("%d\n", y); return 0; }
Attempts:
2 left
💡 Hint
Bitwise NOT flips all bits. For zero, all bits flipped become all ones, which is -1 in two's complement.
✗ Incorrect
In two's complement, ~0 flips all bits to 1, which represents -1 as a signed integer.
🔧 Debug
advanced2:00remaining
Why does this bitwise NOT code produce unexpected output?
This code is intended to print the maximum unsigned integer value after applying bitwise NOT to 0, but it prints -1 instead. Why?
C
#include <stdio.h> int main() { unsigned int x = 0; unsigned int y = ~x; printf("%d\n", y); return 0; }
Attempts:
2 left
💡 Hint
The issue is with how printf interprets the unsigned value when using the wrong format specifier.
✗ Incorrect
The ~ operator on unsigned int 0 produces UINT_MAX (all bits 1). However, printf("%d", y) passes an unsigned int to a format expecting signed int, leading to undefined behavior. In practice, on two's complement systems, it prints -1.
❓ Predict Output
advanced2:00remaining
What is the output of this bitwise NOT on a 16-bit unsigned short?
Given this C code, what will be printed?
C
#include <stdio.h> int main() { unsigned short x = 0x00FF; // 255 decimal unsigned short y = ~x; printf("%u\n", y); return 0; }
Attempts:
2 left
💡 Hint
Bitwise NOT flips all bits in 16 bits. 0x00FF flipped becomes 0xFF00.
✗ Incorrect
0x00FF in binary is 0000000011111111. Flipping all bits gives 1111111100000000, which is 65280 in decimal.
❓ Predict Output
expert2:00remaining
What is the output of this bitwise NOT on a signed char with negative value?
Analyze this C code and determine what it prints.
C
#include <stdio.h> int main() { signed char x = -1; signed char y = ~x; printf("%d\n", y); return 0; }
Attempts:
2 left
💡 Hint
Bitwise NOT flips all bits. For -1 in two's complement, all bits are 1, flipping gives all 0 bits.
✗ Incorrect
In two's complement, -1 is represented by all bits set to 1. Bitwise NOT flips all bits to 0, which is 0 as a signed char.