Bird
Raised Fist0
Cprogramming~20 mins

Bitwise NOT in C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitwise NOT 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 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;
}
A240
B15
C0
D255
Attempts:
2 left
💡 Hint
Remember that bitwise NOT flips all bits. For an 8-bit unsigned char, 0b00001111 becomes 0b11110000.
❓ Predict Output
intermediate
2: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;
}
A0
B-1
C1
D2147483647
Attempts:
2 left
💡 Hint
Bitwise NOT flips all bits. For zero, all bits flipped become all ones, which is -1 in two's complement.
🔧 Debug
advanced
2: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;
}
AThe bitwise NOT operator (~) doesn't work correctly on unsigned integers.
BThere is integer overflow when applying ~ to 0.
CThe printf format specifier "%d" expects a signed int, and the bit pattern of UINT_MAX (all 1s) is interpreted as -1 in two's complement.
Dunsigned int y = ~x promotes x to signed before applying ~.
Attempts:
2 left
💡 Hint
The issue is with how printf interprets the unsigned value when using the wrong format specifier.
❓ Predict Output
advanced
2: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;
}
A65280
B255
C65535
D0
Attempts:
2 left
💡 Hint
Bitwise NOT flips all bits in 16 bits. 0x00FF flipped becomes 0xFF00.
❓ Predict Output
expert
2: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;
}
A127
B-1
C-128
D0
Attempts:
2 left
💡 Hint
Bitwise NOT flips all bits. For -1 in two's complement, all bits are 1, flipping gives all 0 bits.

Practice

(1/5)
1. What does the bitwise NOT operator ~ do to an integer in C?
easy
A. It adds 1 to the integer.
B. It shifts all bits to the left by one position.
C. It sets all bits to zero.
D. It flips every bit of the integer, changing 0s to 1s and 1s to 0s.

Solution

  1. Step 1: Understand the bitwise NOT operator

    The bitwise NOT operator ~ in C flips every bit of the integer. This means all 0 bits become 1, and all 1 bits become 0.
  2. Step 2: Compare with other bitwise operations

    Unlike shifting or adding, ~ only toggles bits without changing their positions or values numerically.
  3. Final Answer:

    It flips every bit of the integer, changing 0s to 1s and 1s to 0s. -> Option D
  4. Quick Check:

    Bitwise NOT = flip bits [OK]
Hint: Remember: ~ flips bits, not shifts or adds [OK]
Common Mistakes:
  • Confusing ~ with bit shift operators << or >>
  • Thinking ~ adds or subtracts from the number
  • Assuming ~ sets bits to zero instead of flipping
2. Which of the following is the correct syntax to apply bitwise NOT to an integer variable x in C?
easy
A. int y = ~x;
B. int y = not x;
C. int y = !x;
D. int y = x~;

Solution

  1. Step 1: Recall the bitwise NOT operator syntax

    In C, the bitwise NOT operator is the tilde symbol ~ placed before the variable.
  2. Step 2: Check each option

    int y = not x; uses not which is not valid in C. int y = !x; uses logical NOT !. int y = x~; has incorrect placement of ~. Only int y = ~x; correctly uses ~x.
  3. Final Answer:

    int y = ~x; -> Option A
  4. Quick Check:

    Bitwise NOT syntax = ~x [OK]
Hint: Use ~ before variable for bitwise NOT [OK]
Common Mistakes:
  • Using 'not' instead of ~
  • Confusing logical NOT (!) with bitwise NOT (~)
  • Placing ~ after the variable
3. What is the output of the following C code?
int x = 5;
int y = ~x;
printf("%d", y);
medium
A. 5
B. -6
C. -5
D. 6

Solution

  1. Step 1: Convert 5 to binary and apply bitwise NOT

    5 in binary (32-bit) is 00000000 00000000 00000000 00000101. Applying ~ flips bits to 11111111 11111111 11111111 11111010.
  2. Step 2: Interpret the result as a signed integer

    The result is the two's complement representation of -6.
  3. Final Answer:

    -6 -> Option B
  4. Quick Check:

    ~5 = -6 in two's complement [OK]
Hint: ~x equals -(x+1) for signed integers [OK]
Common Mistakes:
  • Thinking ~5 equals 6 instead of -6
  • Ignoring two's complement representation
  • Confusing bitwise NOT with logical NOT
4. Identify the error in the following C code snippet:
int x = 10;
int y = ~ x;
printf("%d", y);
medium
A. There is a syntax error due to space between ~ and x.
B. The code will print 9 instead of expected -11.
C. No error; the code works and prints -11.
D. The printf format specifier is incorrect.

Solution

  1. Step 1: Check syntax of bitwise NOT usage

    In C, spaces between operators and operands are allowed. So ~ x is valid and equivalent to ~x.
  2. Step 2: Verify output and printf usage

    Applying ~10 results in -11. The printf format specifier %d is correct for int.
  3. Final Answer:

    No error; the code works and prints -11. -> Option C
  4. Quick Check:

    Space allowed after ~, output = -11 [OK]
Hint: Spaces don't break ~ operator usage [OK]
Common Mistakes:
  • Thinking space causes syntax error
  • Expecting positive output instead of negative
  • Confusing format specifier for int
5. Given a signed 8-bit integer n, which expression correctly computes the bitwise NOT of n and then adds 1, effectively calculating the two's complement negation of n in C?
hard
A. ~n + 1
B. ~(n + 1)
C. -~n
D. -n + 1

Solution

  1. Step 1: Understand two's complement negation

    Two's complement negation of a number n is computed as ~n + 1.
  2. Step 2: Analyze each option

    ~n + 1 matches the formula. ~(n + 1) negates after adding 1, which is incorrect. -~n negates the bitwise NOT, not the number. -n + 1 is arithmetic negation plus 1, which is not two's complement negation.
  3. Final Answer:

    ~n + 1 -> Option A
  4. Quick Check:

    Two's complement negation = ~n + 1 [OK]
Hint: Two's complement negation = ~n + 1 [OK]
Common Mistakes:
  • Using -(~n) instead of ~n + 1
  • Applying ~ after adding 1
  • Confusing arithmetic negation with bitwise negation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard