0
0
Cprogramming~10 mins

Bitwise NOT in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bitwise NOT
Start with integer value
Apply bitwise NOT (~) operator
Flip each bit: 0->1, 1->0
Result is new integer with flipped bits
Use or print the result
End
The bitwise NOT operator flips every bit of an integer, turning 0s into 1s and 1s into 0s, producing a new integer.
Execution Sample
C
int x = 5;
int y = ~x;
printf("%d", y);
This code flips all bits of 5 and prints the resulting integer.
Execution Table
StepVariableValue (decimal)Value (binary 8-bit)OperationResult (binary)Result (decimal)
1x500000101Initial value000001015
2yN/AN/AApply ~x (bitwise NOT)11111010-6
3y-611111010Print y11111010-6
💡 Program ends after printing the bitwise NOT of 5, which is -6 in two's complement.
Variable Tracker
VariableStartAfter Step 2Final
x555
yundefined-6-6
Key Moments - 2 Insights
Why does ~5 become -6 instead of a large positive number?
Because integers use two's complement representation, flipping bits of 5 (00000101) results in 11111010, which is -6 in two's complement. See execution_table step 2.
Why do we show 8 bits in binary when integers are usually 32 bits?
For simplicity and clarity, we show 8 bits here. The actual integer has more bits, but the principle of flipping each bit is the same. The sign bit determines if the number is negative.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the binary result of applying ~ to 5?
A00000101
B11111010
C11111101
D00011010
💡 Hint
Check the 'Result (binary)' column in step 2 of execution_table.
At which step is the variable y assigned a value?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the 'Operation' column where ~x is applied and y gets its value.
If x was 0 instead of 5, what would be the decimal value of ~x?
A1
B0
C-1
D-0
💡 Hint
Remember that ~0 flips all bits to 1, which is -1 in two's complement.
Concept Snapshot
Bitwise NOT (~) flips every bit of an integer.
Example: ~5 flips 00000101 to 11111010.
In two's complement, this results in -6.
Useful for low-level bit manipulation.
Always remember sign bit affects result.
Full Transcript
The bitwise NOT operator (~) takes an integer and flips all its bits. For example, starting with 5, which is 00000101 in binary (8-bit shown for simplicity), applying ~ flips every bit to 11111010. In two's complement representation, this binary number equals -6. The program stores this result in variable y and prints it. This shows how bitwise NOT changes positive numbers into negative ones by flipping bits, including the sign bit. Understanding two's complement is key to interpreting the result correctly.