Definition:
It is a bitwise operator, called the Bitwise NOT operator.
It works on the binary representation of a number.
It takes a number (operand) and converts it into binary form.
It changes 0 into 1 and 1 into 0. For this reason, the result becomes negative because computers use a special method called 2’s complement.
Now you will clearly understand this with the help of the diagram.
15 → 0000 1111 ~15 → 1111 0000 Result = -16
Syntax:
result = ~operand
Example:
a = 15 # binary: 1111
result = ~a
print(result)
Output:
Explanation:
15 → 0000 1111
~15 → 1111 0000 (2’s complement form)
= -16
Exercise
Use Bitwise NOT operator with value 8 and print the result using Python.
