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