Python Bitwise Operator Example

Bitwise Operator

Bitwise Operator will do the operation between binary values.

 Bitwise Operator & (And) | (Or) ^ (Xor) ~ (Not) << (Left Shift) >> (Right Shift)
Program
x = 5 #0000 0101 y = 9 #0000 1001 print(x & y) # 0000 0101 & 0000 1001 = 0000 0001 = 1 print(x | y) # 0000 0101 | 0000 1001 = 0000 1101 = 13 print(x ^ y) # 0000 0101 ^ 0000 1001 = 0000 1100 = 12 print(~x) # ~ 0000 0101 = 1111 1010 = 250, Therefore 250-256 = -6 print(x << 1) # 0000 0101 (On Left Shifting by 1)= 0000 1010 = 10 print(x << 3) # 0000 0101 (On Left Shifting by 3)= 0010 1000 = 40 print(x >> 1) # 0000 0101 (On Right Shifting by 1)= 0000 0010 = 2 print(x >> 3) # 0000 0101 (On Right Shifting by 3)= 0000 0000 = 0
Input
Output

Similar Program

# Program Language
1 Python 3
2 Python 3
3 Python 3
4 Python 3
5 Python 3
6 Python 3
7 Python 3
8 Python 3
9 Python 3
10 Python 3
11 Python 3
12 Python 3
13 Python 3
14 Python 3
15 Python 3
16 Python 3
17 Python 3
18 Python 3
19 Python 3
20 Python 3
# Program Language