Python Bitwise Operators & Operations

Python has AND, OR, XOR, NOT, LEFT SHIFT and RIGHT SHIFT Bitwise opeartors for different operations on integer numbers. Following section has individual explanation for each bitwise operators with operations.

  1. & : Bitwise AND operation
  2. | : Bitwise OR operation
  3. ^ : Bitwise XOR operation
  4. ~ : Bitwise NOT operation
  5. << : Bitwise LEFT SHIFT operation
  6. >> : Bitwise RIGHT SHIFT operation

& (Bitwise AND Opeartions)

To understand Bitwise AND operations, you must know the following truth table for AND gate or AND operations

x y x & y
0 0 0
0 1 0
1 0 0
1 1 1

See following code to understand Bitwise AND (&) in Python


>>> 22 & 28
20
>>> bin(22)
'0b10110'
>>> bin(28)
'0b11100'
>>> bin(20)
'0b10100'

Here 0b is prefix for binary. Number 22 in binary is 10110 and Number 28 in binary is 11100. Their AND (&) operation is illustrated below:

1 0 1 1 0 (22)
1 1 1 0 0 (28)
--------------
1 0 1 0 0 (20)

Note: See bitwise AND operation according to above truth table for AND gate.

| (Bitwise OR Opeartions)

To understand Bitwise OR operations, you must know the following truth table for OR gate or OR operations

x y x | y
0 0 0
0 1 1
1 0 1
1 1 1

See following code to understand Bitwise OR ( | ) in Python


>>> 22 | 28
30
>>> bin(22)
'0b10110'
>>> bin(28)
'0b11100'
>>> bin(30)
'0b11110'

Here 0b is prefix for binary. Number 22 in binary is 10110 and Number 28 in binary is 11100. Their OR ( | ) operation is illustrated below:

1 0 1 1 0 (22)
1 1 1 0 0 (28)
--------------
1 1 1 1 0 (30)

Note: See bitwise OR operation according to above truth table for OR gate.

^ (Bitwise XOR Opeartions)

To understand Bitwise XOR operations, you must know the following truth table for XOR gate or XOR operations

x y x ^ y
0 0 0
0 1 1
1 0 1
1 1 0

See following code to understand Bitwise XOR ( ^ ) in Python


>>> 22 ^ 28
10
>>> bin(22)
'0b10110'
>>> bin(28)
'0b11100'
>>> bin(10)
'0b1010'

Here 0b is prefix for binary. Number 22 in binary is 10110 and Number 28 in binary is 11100. Their XOR ( ^ ) operation is illustrated below:

1 0 1 1 0 (22)
1 1 1 0 0 (28)
--------------
0 1 0 1 0 (10)

Note: See bitwise XOR operation according to above truth table for XOR gate.

~ (Bitwise NOT Opeartions)

To understand Bitwise NOT operations, you must know the following truth table for NOT gate or NOT operations

x ~ x
0 1
1 0

See following code to understand Bitwise NOT ( ~ ) in Python


>>> ~ 22
-23
>>> bin(22)
'0b10110'
>>> bin(-23)
'-0b10111'

Here 0b is prefix for binary. Number 22 in binary is 10110. ~22 = -23 but binary for -23 is not 01001 as we expected from truth table, right? So, what happened here? This is because Negative Numbers in Computer are Represented Using 2's Complement.


<< (Bitwise SHIFT LEFT Opeartions)

To understand Bitwise SHIFT LEFT operations, please look at the following code:


>>> 22 << 2
88
>>> bin(22)
'0b10110'
>>> bin(88)
'0b1011000'

Here 0b is prefix for binary. Number 22 in binary is 10110. Here 22 << 2 indicates binary representation for 22 is left shifted by 2 position. See following illustration:

22 => 1 0 1 1 0

1 position LEFT SHIFT => 1 0 1 1 0 0
2 position LEFT SHIFT => 1 0 1 1 0 0 0

Final 1011000 is 88


<< (Bitwise SHIFT RIGHT Opeartions)

To understand Bitwise SHIFT RIGHT operations, please look at the following code:


>>> 22 >> 2
5
>>> bin(22)
'0b10110'
>>> bin(5)
'0b101'

Here 0b is prefix for binary. Number 22 in binary is 10110. Here 22 << 2 indicates binary representation for 22 is right shifted by 2 position. See following illustration:

22 => 1 0 1 1 0

1 position RIGHT SHIFT => 0 1 0 1 1  
2 position RIGHT SHIFT => 0 0 1 0 1  

Final 00101 is 5


See full reference in python documentation.