Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is tilde (~) operator in Python?
In Python, the bitwise operator ~ (pronounced as tilde) is a complement operator. It takes one bit operand and returns its complement. If the operand is 1, it returns 0, and if it is 0, it returns 1.
For example, consider the 4-bit binary number (1100)2. By performing the tilde operation, each bit is flipped and results in (0011)2.
Syntax
result = ~operand
Tilde Operation on Decimal Numbers
When we perform a tilde operation on decimal numbers, the following steps are involved ?
- Convert the decimal number to binary, including the sign. If the number is positive, place 0 in the MSB (Most Significant Bit); if it is negative, place 1 in the MSB.
- Perform the tilde operation (bitwise NOT), which flips all bits of the binary number.
- Convert the resulting binary number back to decimal. While doing this, consider the MSB as the sign bit (negative if it's 1, positive if it's 0).
Example
In the following example, we perform the tilde operation on the decimal number 5 by following the steps outlined above ?
- The 4-bit binary representation of 5 is 0101. Since the number is positive, the MSB (most significant bit) is 0.
- Apply the tilde operation by flipping all the bits: 0101 ? 1010.
- Convert 1010 back to decimal using two's complement interpretation:
-23 X 1 + 22 X 0 + 21 X 1 + 20 X 0 = -8 + 0 + 2 + 0 = -6
var1 = 5
print("Binary code of 5:", bin(var1)[2:]) # binary format of var1 excluding 0b
tilde_res = ~(var1) # tilde operation on var1
print("Tilde operation on 5:", tilde_res)
Following is the output of the above code ?
Binary code of 5: 101 Tilde operation on 5: -6
Mathematical Formula
The tilde operator follows the formula: ~x = -(x + 1). This is because Python uses two's complement representation for integers.
numbers = [5, -3, 0, 10]
for num in numbers:
tilde_result = ~num
formula_result = -(num + 1)
print(f"~{num} = {tilde_result}, -(({num}) + 1) = {formula_result}")
~5 = -6, -((5) + 1) = -6 ~-3 = 2, -((-3) + 1) = 2 ~0 = -1, -((0) + 1) = -1 ~10 = -11, -((10) + 1) = -11
Using Tilde Operator with Boolean
In Python, when we apply the tilde operation on the boolean, it might work a bit differently, as True is considered as 1 and False is considered as 0.
Example
In the following example, we perform the tilde operation on Boolean values. Since True == 1 and False == 0, we apply bitwise NOT (~) to those integer values ?
The binary representation of 1 (True) is 01, and for 0 (False), it is 00. When we apply the tilde operator (~), it flips all the bits ?
- ~1 ? 10 ? -21 X 1 + 20 X 0 = -2
- ~0 ? 11 ? -21 X 1 + 20 X 1 = -1
The 1 in the MSB (most significant bit) indicates a negative number in two's complement form, while 0 indicates a positive number. So, the tilde operation on booleans in Python results in -2 for True and -1 for False.
print("The tilde operation on True:", ~True) # tilde operation on True
print("The tilde operation on False:", ~False) # tilde operation on False
print("True as integer:", int(True))
print("False as integer:", int(False))
Following is the output of the above code ?
The tilde operation on True: -2 The tilde operation on False: -1 True as integer: 1 False as integer: 0
Common Use Cases
The tilde operator is commonly used in bit manipulation tasks, array indexing, and boolean operations ?
# Bit manipulation
num = 12 # Binary: 1100
print(f"Original: {num}, Binary: {bin(num)}")
print(f"Tilde result: {~num}, Binary: {bin(~num & 0xFF)}")
# Array indexing (negative indexing equivalent)
data = [10, 20, 30, 40, 50]
print(f"Last element using ~0: {data[~0]}") # Same as data[-1]
print(f"Second last using ~1: {data[~1]}") # Same as data[-2]
Original: 12, Binary: 0b1100 Tilde result: -13, Binary: 0b11110011 Last element using ~0: 50 Second last using ~1: 40
Conclusion
The tilde (~) operator in Python performs bitwise NOT operation, flipping all bits of a number. It follows the formula ~x = -(x + 1) due to two's complement representation. This operator is useful for bit manipulation and creative indexing techniques.
