Python Bitwise Operators

By Arvind Rai, March 31, 2019
Bitwise operators are used with integers. Bitwise operators supported by Python are OR, XOR, AND, Left-shift, Right-shift and ones Complement. Find the detail.
Operator NameOperationsResult
Bitwise OR (|)x | ybitwise or of x and y
Bitwise XOR (^)x ^ ybitwise exclusive or of x and y
Bitwise AND (&)x & ybitwise and of x and y
Bitwise Left-shift (<<)x << nx shifted left by n bits
Bitwise Right-shift (>>)x >> nx shifted right by n bits
Bitwise ones Complement (~)~xthe bits of x inverted

For the demo we are using Python 3.7.0 in our example. Now find the Python bitwise operators in detail.

1. Bitwise OR (|)

A bitwise OR (|) takes two equal-length binary representations and performs the logical OR operation on each pair of the corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1.
| Operator Result
1 | 1 1
1 | 0 1
0 | 1 1
0 | 0 0
Find the example.
>>> 3 | 5
7
>>> bin(3)
'0b11'
>>> bin(5)
'0b101'
>>> bin(7)
'0b111'
>>>
>>> 6 | 4
6
>>> bin(6)
'0b110'
>>> bin(4)
'0b100'
>>>

2. Bitwise XOR (^)

A bitwise XOR (^) takes two equal-length binary representations and performs the logical exclusive OR operation on each pair of the corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.
^ Operator Result
1 ^ 1 0
1 ^ 0 1
0 ^ 1 1
0 ^ 0 0
Find the example.
>>> 4 ^ 2
6
>>> bin(4)
'0b100'
>>> bin(2)
'0b10'
>>> bin(6)
'0b110'
>>>
>>> 7 ^ 8
15
>>> bin(7)
'0b111'
>>> bin(8)
'0b1000'
>>> bin(15)
'0b1111'
>>>

3. Bitwise AND (&)

A bitwise AND (&) operator takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits. The result is equivalent to multiplying them.
& Operator Result
1 & 1 1
1 & 0 0
0 & 1 0
0 & 0 0
Find the example.
>>> 5 & 9
1
>>> bin(5)
'0b101'
>>> bin(9)
'0b1001'
>>> bin(1)
'0b1'
>>>
>>> 7 & 3
3
>>> bin(7)
'0b111'
>>> bin(3)
'0b11'
>>>

4. Bitwise Left-shift (<<)

Bitwise Left-shift (<<) operator shifts the bits left by given number. x << n returns x with the bits shifted to the left by n places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by pow(2, n).
Find the example.
>>> 5 << 2
20
>>> bin(5)
'0b101'
>>> bin(20)
'0b10100'
>>>
>>> 12 << 6
768
>>> bin(12)
'0b1100'
>>> bin(768)
'0b1100000000'
>>>

5. Bitwise Right-shift (>>)

Bitwise Right-shift (>>) operator shifts the bits right by given number. x >> n returns x with the bits shifted to the right by n places. This is the same as dividing x by pow(2, n).
Find the example.
>>> 4 >> 2
1
>>> bin(4)
'0b100'
>>> bin(2)
'0b10'
>>> bin(1)
'0b1'
>>>
>>> 13 >> 2
3
>>> bin(13)
'0b1101'
>>> bin(2)
'0b10'
>>> bin(3)
'0b11'
>>>

6. Bitwise ones Complement (~)

~x returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.
Find the example.
>>> 13 >> 2
3
>>> bin(13)
'0b1101'
>>> bin(2)
'0b10'
>>> bin(3)
'0b11'
>>>
Bitwise Operations on Integer Types
Python Bitwise Operators
Bitwise operation