
Bitwise operators in Python perform operations on the individual bits of integers. They work with binary values, where each bit is represented by 0 or 1. These operators are commonly used in bit manipulation, binary calculations, permission flags, and low-level programming.
Python provides six bitwise operators: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Understanding how these operators work helps developers manipulate integer values efficiently and understand how data is processed at the binary level.
Bitwise operators are symbols that perform logical operations on the binary representations of integers. Instead of comparing entire numbers, they evaluate individual bits according to the rules of each operator.
For example, the integer 5 is represented as 0101 in four-bit binary notation, while 3 is represented as 0011. A bitwise operation compares or shifts these binary digits to produce a new integer.
a = 5
b = 3
print(a & b) # AND: 1
print(a | b) # OR: 7
print(a ^ b) # XOR: 6
Output:
1
7
6
Each operator produces a different result because it applies a different rule to the binary digits.
Python has six main bitwise operators. The following table summarizes their symbols, purposes, and results.
| Operator | Symbol | Description | Example | Output |
|---|---|---|---|---|
| Bitwise AND | & |
Returns 1 when both corresponding bits are 1. |
5 & 3 |
1 |
| Bitwise OR | | |
Returns 1 when at least one corresponding bit is 1. |
5 | 3 |
7 |
| Bitwise XOR | ^ |
Returns 1 when corresponding bits differ. |
5 ^ 3 |
6 |
| Bitwise NOT | ~ |
Inverts the bits of an integer. | ~5 |
-6 |
| Left Shift | << |
Shifts bits to the left. | 5 << 2 |
20 |
| Right Shift | >> |
Shifts bits to the right. | 20 >> 2 |
5 |
The examples use decimal integers. Their binary representations help explain how each operator produces its result.
The bitwise XOR (exclusive OR) operator in Python uses the caret symbol (^). It compares corresponding bits of two integers and returns 1 when the bits differ and 0 when they match.
XOR is useful for toggling individual bits, comparing binary values, and performing certain parity calculations.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a ^ b
print(result)
Output:
6
The operation works as follows:
Binary of 5 |
0 |
1 |
0 |
1 |
|---|---|---|---|---|
Binary of 3 |
0 |
0 |
1 |
1 |
| XOR result | 0 |
1 |
1 |
0 |
The resulting binary value is 0110, which equals 6 in decimal.
The bitwise AND operator (&) compares corresponding bits of two integers. A result bit is 1 only when both input bits are 1. Otherwise, the result bit is 0.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a & b
print(result)
Output:
1
The binary operation 0101 & 0011 produces 0001, which represents the decimal value 1.
AND is often used to check whether a particular bit is set. For example, value & 1 checks the least significant bit of an integer.
The bitwise NOT operator (~) inverts the bits of an integer, changing 0 to 1 and 1 to 0 in its conceptual binary representation.
Python integers behave as though they have an unlimited number of sign-extension bits. As a result, the NOT operator follows the rule ~x == -(x + 1) for integers.
a = 5
result = ~a
print(result)
Output:
-6
The result is -6 because ~5 is equivalent to -(5 + 1). This is an important distinction when working with Python's negative integers.
The bitwise OR operator (|) compares corresponding bits of two integers. It returns 1 if either input bit is 1, and returns 0 only when both input bits are 0.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a | b
print(result)
Output:
7
The binary operation 0101 | 0011 produces 0111, which equals 7 in decimal.
OR is useful when setting specific bits in a value without changing other bits that are already set.
The bitwise left shift operator (<<) moves the bits of an integer to the left by a specified number of positions. For non-negative integers, shifting left by n positions is equivalent to multiplying by (2^n).
a = 5
result = a << 2
print(result)
Output:
20
The binary representation of 5 is 0101. Shifting it two positions to the left produces 10100, which represents 20 in decimal.
Left shifts are useful for manipulating binary data and working with values stored in specific bit positions.
The bitwise right shift operator (>>) moves the bits of an integer to the right by a specified number of positions. For non-negative integers, it is equivalent to integer division by (2^n), with the fractional part discarded.
a = 20
result = a >> 2
print(result)
Output:
5
The binary representation of 20 is 10100. Shifting it two positions to the right produces 00101, which represents 5 in decimal.
For negative integers, Python's right shift behaves like floor division by a power of two, rather than truncating toward zero.
Bitwise operators in Python allow developers to manipulate individual bits of integer values. The AND operator checks matching set bits, OR combines set bits, XOR identifies differing bits, and NOT inverts bits. Left and right shift operators move bits between positions.
Understanding these six operators provides a foundation for binary arithmetic, bit masking, flag management, and other programming tasks involving low-level data manipulation.