Explains Python bitwise operators, their behavior, examples, shifting, compound assignments, and two’s complement implications for flags, masks, and low-level integer manipulation.
Bitwise operators let you manipulate individual bits of integer values by operating on their binary representations. These operators are commonly used for flags, masks, low-level data manipulation, and performance-sensitive code.
How each operator works:
Operator
Symbol
Description
Example
Bitwise AND
&
Returns 1 for each bit position where both operands have 1
15 & 22 → 6
Bitwise OR
`
`
Returns 1 where at least one operand has 1
`15
22→31`
Bitwise XOR
^
Returns 1 where exactly one operand has 1 (exclusive OR)
15 ^ 22 → 25
Bitwise NOT
~
Flips every bit. In Python this produces the two’s‑complement negative: ~n == -n - 1
~22 → -23
Left shift
<<
Moves bits left (multiplying non-negative integers by powers of two)
22 << 1 → 44
Right shift
>>
Moves bits right (dividing non-negative integers by powers of two using floor division)
22 >> 1 → 11
Note: Bitwise operators operate on integers only; they are not defined for floating-point numbers in Python. For official behavior and details, see the Python documentation on numeric types and bitwise operations.Bitwise AND example (15 & 22)15 in binary: 00001111
22 in binary: 00010110Bitwise AND compares each corresponding bit and returns 1 only where both bits are 1:
Copy
print(15 & 22) # 6
The result 6 corresponds to binary 00000110.Bitwise OR example (15 | 22)Bitwise OR returns 1 for a bit position if either (or both) input bits are 1:
Copy
print(15 | 22) # 31
The result 31 corresponds to binary 00011111.Bitwise XOR example (15 ^ 22)Bitwise XOR returns 1 only when exactly one of the bits is 1.
Copy
print(15 ^ 22) # 25
The result 25 corresponds to binary 00011001.Bitwise NOT example (~22)Bitwise NOT flips every bit. In Python this yields the two’s‑complement negative value; the identity ~n == -n - 1 holds for integers:
Copy
print(~22) # -23
Compound assignment formsYou can combine bitwise operations with assignment to update a variable in place. The long and abbreviated forms are equivalent:
Bit shiftingBit shifting moves bits left or right by a specified number of positions and is equivalent to multiplication or integer division by powers of two for non-negative integers.
Right shift (>>): shifts bits to the right. Each shift right by 1 divides the integer by 2 using floor division for non-negative values.
Left shift (<<): shifts bits to the left. Each shift left by 1 multiplies the integer by 2.
Bitwise operators are useful for flags, masks, and efficient low-level manipulations. Remember that Python integers are unbounded and bitwise operations follow two’s‑complement logic, so ~n equals -n - 1.
Recap
Logical operators (and, or, not) operate on boolean expressions and return boolean results — useful for control flow and conditions.
Bitwise operators (&, |, ^, ~) operate at the bit level on integers and return integer results reflecting bitwise changes.
Bit shifts (<<, >>) move bits left or right and correspond to multiplication or integer division by powers of two for non-negative integers.