bitwise operator
In Python, a bitwise operator works on the individual bits of an integer’s binary representation instead of on its value as a whole number. Python has six of them: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift). You’ll use them when you pack several flags into one integer, apply a bitmask, or pull fields out of binary data.
The binary operators &, |, and ^, so called because they take two operands, line up two integers and combine each pair of bits. AND sets a result bit only where both inputs have a 1, OR sets it where either one does, and the XOR operator sets it where the inputs differ. With 12 (0b1100) and 10 (0b1010), you get 12 & 10 == 8, 12 | 10 == 14, and 12 ^ 10 == 6.
Try it yourself by picking an operator and flipping individual bits to see how each result bit is computed:
The shift operators move every bit left or right by a given count. Shifting left by n multiplies by 2 ** n, and shifting right by n floor-divides by 2 ** n, so 5 << 2 is 20 and 20 >> 2 is 5. A negative shift count raises ValueError.
The unary ~ operator inverts every bit, and for integers ~x always equals -x - 1. That’s why ~5 is -6. Python’s integers have no fixed width, so bitwise operations behave as if negative numbers used two’s complement with an infinite supply of leading sign bits.
The binary bitwise operators bind more loosely than arithmetic but more tightly than comparisons, so x & 1 == 0 means (x & 1) == 0. Each binary operator also has an augmented assignment form, such as |= or <<=. Sets reuse &, |, and ^ for intersection, union, and symmetric difference. Your own classes can support these operators by defining magic methods like .__and__() and .__or__().
Example
Say you’re working with a color stored as a single hex integer, and you need its red, green, and blue channels as separate values:
>>> color = 0xFF8800
>>> red = (color >> 16) & 0xFF
>>> green = (color >> 8) & 0xFF
>>> blue = color & 0xFF
>>> red, green, blue
(255, 136, 0)
>>> hex((red << 16) | (green << 8) | blue)
'0xff8800'
Each channel occupies eight bits of the integer. Shifting right slides the channel you want into the lowest eight bits, and & 0xFF masks off everything above them. Going the other way, << moves each channel back into its slot and | merges the three slots into one integer.
Related Resources
Tutorial
Bitwise Operators in Python
Learn how to use Python's bitwise operators to manipulate individual bits of data at the most granular level.
For additional information on related topics, take a look at the following resources:
- Binary, Bytes, and Bitwise Operators in Python (Course)
- Operators and Expressions in Python (Tutorial)
- Numbers in Python (Tutorial)
- Bytes Objects: Handling Binary Data in Python (Tutorial)
- Working With the Python operator Module (Tutorial)
- Bitwise Operators in Python (Quiz)
- Python Operators and Expressions (Course)
- Operators and Expressions in Python (Quiz)
- Python Bytes (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Nov. 13, 2026