Python has six bitwise operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift) and >> (right shift). They work on the binary representation of integers, one bit at a time, and the same symbols are overloaded for sets and booleans. This guide explains how the Python bitwise operators behave, including negative numbers, which surprise most people.
Python integers have arbitrary size and behave as if they were stored in two's complement with infinitely many sign bits. That one rule explains every result below. Examples use Python 3.13 and 3.14.
Take a = 0b1100 (12) and b = 0b1010 (10).
| Operator | Name | Example | Result | Binary |
|---|---|---|---|---|
a & b | AND | 12 & 10 | 8 | 0b1000 |
a | b | OR | 12 | 10 | 14 | 0b1110 |
a ^ b | XOR | 12 ^ 10 | 6 | 0b0110 |
~a | NOT | ~12 | -13 | (see below) |
a << n | left shift | 12 << 2 | 48 | 0b110000 |
a >> n | right shift | 12 >> 2 | 3 | 0b11 |
A left shift by n multiplies by 2**n. A right shift by n floors the division by 2**n. Python never overflows: 1 << 100 is a perfectly valid integer.
~x flips every bit. With two's complement and an infinite run of sign bits, flipping all bits of x gives -x - 1. So ~0 is -1, ~12 is -13 and ~-1 is 0.
If you want the inverse limited to a width such as 8 bits, mask the result:
bin(-5) prints -0b101, not the bit pattern. To get the fixed-width form, AND the number with a mask.
>> is an arithmetic shift: the sign bit is copied in. Negative numbers floor towards negative infinity.
There is no separate unsigned shift operator like >>> in Python. Shift counts must be non-negative: 1 << -1 raises ValueError: negative shift count.
The classic flag operations use one bit per option.
Other idioms that come up often:
int.bit_count() was added in Python 3.10 and replaces the old bin(n).count("1") trick. For named flags, the standard library offers enum.IntFlag:
In Python, comparison operators bind looser than the bitwise operators, so x & 1 == 0 means (x & 1) == 0. In C it would mean x & (1 == 0). The order among bitwise operators, from tighter to looser, is: shifts, then &, then ^, then |. Arithmetic binds tighter than shifts, so 1 + 2 << 3 is (1 + 2) << 3, which is 24. When in doubt, add parentheses.
For sets, & is intersection, | is union and ^ is symmetric difference. For bool, &, | and ^ return a bool, but ~True is -2, because True is the integer 1. Bitwise inversion on bool is deprecated since Python 3.12, so use not for logic.
Bitwise operators accept only integers (and types that define them). 1.5 & 1 raises TypeError.
& is bitwise and evaluates both sides. and is logical, short-circuits, and returns one of its operands. Use and for conditions and & for bits, sets and combining NumPy or pandas boolean masks.
Flipping all the bits of an integer with infinite sign bits gives -x - 1, so ~5 is -6. Mask with & 0xFF if you want an 8-bit result.
No. Mask first, for example (x & 0xFFFFFFFF) >> n, to emulate a 32-bit unsigned shift.
Use n.bit_count() on Python 3.10 or later. On older versions use bin(n).count("1").
&, |, ^, ~, << and >> operate on arbitrarily large integers as if they used infinite two's complement. Remember ~x == -x - 1, mask with & 0xFF to see fixed-width results, and rely on >> preserving the sign. Use shifts with |, & and ~ for flags, bit_count() for population count, and parentheses whenever shifts and bitwise operators mix.