Bitwise operators perform operations on the binary (bit) representation of integers.
Background: Each integer is first written as a binary number that is a sequence of digits 0 or 1. For example:
- 0 is written as “0”
- 1 is written as “1”
- 2 is written as “10”
- 3 is written as “11”
- 4 is written as “100”
- 5 is written as “101”
The bitwise operators are then applied on those binary representations. The resulting binary number is then converted back to the integer representation (decimal number).
In the following table, we exemplify all bit-wise operators with variables x
and y
defined as follows:
>>> x = 4 # 100 >>> y = 5 # 101
As a comment, we also provide the binary representation 100
for the decimal integer 4
, and 101
for the decimal integer 5
.
Operator | Name | Description | Example |
---|---|---|---|
x = 4, y = 5 | |||
& | Bitwise AND | Performs logical AND on a bit-by-bit basis | x & y |
| | Bitwise OR | Performs logical OR operation on a bit-by-bit basis | x | y |
~ | Bitwise NOT | Performs logical NOT on a bit-by-bit basis, inverting each bit so that 0 becomes 1 and 1 becomes 0. Same as -x-1 . | ~x |
^ | Bitwise XOR | Performs logical “exclusive or” operation on a bit-by-bit basis | x ^ y |
>> | Bitwise right shift | Shifts binary of left operand to the right by the number of positions specified in right operand | x >> 2 |
<< | Bitwise left shift | Shifts binary of left operand to the left by the number of positions specified in right operand | x << 2 |
Let’s dive into these bitwise operators one by one:
Bitwise AND
Python’s bitwise AND operator x & y
performs logical AND on each bit position on the binary representations of integers x
and y
. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0. For example, the integer expression 4 & 3 is translated to binaries 0100 & 0011 which results in 0000 because all four input bit positions are different.
In this example, you apply the bitwise AND operator to two integers 32 and 16:
>>> 32 & 16 0
The expression 32 & 16
operates on the bit representations "010000"
(decimal 32) and "001000"
(decimal 16) and performs bitwise AND. As all i-th bit positions are different, the result is 0:
First Operand x | 1 | 0 | 0 | 0 | 0 | 0 |
Second Operand y | 0 | 1 | 0 | 0 | 0 | 0 |
x & y | 0 | 0 | 0 | 0 | 0 | 0 |
For more examples and a more detailed explanation of the operator, check out our detailed blog guide.
Full Guide: Python Bitwise AND Operator
Bitwise OR
Python’s bitwise OR operator x | y
performs logical OR on each bit position on the binary representations of integers x
and y
. Each output bit evaluates to 1 if and only if at least one of the two input bits at the same position are 1. For example, the integer expression 4 | 3
is translated to the binary operation 0100 | 0011
which results in 0111
because for the last three positions at least one bit is 1.
In this example, you apply the bitwise OR operator to two integers 32 and 16:
>>> 32 | 16 48
The expression 32 | 16
operates on the bit representations "010000"
(decimal 32) and "001000"
(decimal 16) and performs bitwise OR. Each "1"
position propagates remains in the result "110000"
(decimal 48):
First Operand x | 1 | 0 | 0 | 0 | 0 | 0 |
Second Operand y | 0 | 1 | 0 | 0 | 0 | 0 |
x | y | 1 | 1 | 0 | 0 | 0 | 0 |
For more examples and a more detailed explanation of the operator, check out our detailed blog guide.
Full Guide: Python Bitwise OR Operator
Bitwise NOT
Python’s bitwise NOT operator ~x
inverts each bit from the binary representation of integer x
so that 0 becomes 1 and 1 becomes 0. This is semantically the same as calculating ~x == -x-1
. For example, the bitwise NOT expression ~0
becomes -1
, ~9
becomes -10
, and ~32
becomes -33
.
In this example, you apply the bitwise NOT operator to integer 32:
>>> ~32 -33
The expression ~32
operates on the bit representations "0100000"
(decimal 32) and performs bitwise NOT resulting in binary "1011111"
. This corresponds to the negative decimal number -33.
x | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
~x | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
How do you transform the binary “1011111” to a decimal number again? By using the following steps:
- Flip each bit back to
0100000
. - Get the corresponding decimal number
32
. - Increase it by one to
33
. - Prefix it with the negative symbol
-33
.
For more examples and a more detailed explanation of the operator, check out our detailed blog guide.
Full Guide: Python Bitwise NOT Operator
Bitwise XOR
Python’s bitwise XOR operator x ^ y
performs logical XOR on each bit position on the binary representations of integers x
and y
. Each output bit evaluates to 1 if and only if exactly one of the two input bits at the same position are 1. For example, the integer expression 4 ^ 3
is translated to the binary operation 0100 ^ 0011
which results in 0111
because for the last three positions exactly one bit is 1.
In this example, you apply the bitwise XOR operator to two integers 32 and 16:
>>> 32 ^ 16 48
The expression 32 ^ 16
operates on the bit representations "0100000"
(decimal 32) and "0010000"
(decimal 16) and performs bitwise XOR resulting in binary "0110000"
(decimal 48):
First Operand x | 1 | 0 | 0 | 0 | 0 | 0 |
Second Operand y | 0 | 1 | 0 | 0 | 0 | 0 |
x ^ y | 1 | 1 | 0 | 0 | 0 | 0 |
For more examples and a more detailed explanation of the operator, check out our detailed blog guide.
Full Guide: Python Bitwise XOR Operator
Bitwise Right-Shift
The Python bitwise right-shift operator x >> n
shifts the binary representation of integer x
by n
positions to the right. It inserts a 0
bit on the left and removes the right-most bit. For example, if you right-shift the binary representation 0101
by one position, you’d obtain 0010
. Semantically, the bitwise right-shift operator is the same as performing integer division by 2**n
.
Here’s a minimal example:
print(8 >> 1) # 4 print(8 >> 2) # 2 print(-3 >> 1) # -2
Let’s dive deeper into the details next!
For more examples and a more detailed explanation of the operator, check out our detailed blog guide.
Full Guide: Python Bitwise Right-Shift Operator
Bitwise Left-Shift
The Python bitwise left-shift operator x << n
shifts the binary representation of integer x
by n
positions to the left. For a positive integer, it inserts a 0
bit on the right and shifts all remaining bits by one position to the left. For example, if you left-shift the binary representation 0101
by one position, you’d obtain 01010
. Semantically, the bitwise left-shift operator x << n
is the same as multiplying the integer x
with 2**n
.
Here’s a minimal example:
print(8 << 1) # 16 print(8 << 2) # 32 print(-3 << 1) # -6
As you go over the article, you can watch my explainer video here:
For more examples and a more detailed explanation of the operator, check out our detailed blog guide.
Full Guide: Python Bitwise Left-Shift Operator