Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do the bitwise operators work in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 26, 2026
6 min read

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.

Table of Contents

  • The six operators at a glance
  • How negative numbers behave
    • Why ~x equals -x - 1
    • Masking to see two's complement
    • Right shift keeps the sign
  • Practical bit manipulation patterns
  • Operator precedence and other types
    • Precedence differs from C
    • Sets and booleans
  • Frequently Asked Questions
    • What is the difference between & and and?
    • Why does ~5 give -6?
    • Is there an unsigned right shift in Python?
    • How do I count set bits?
  • Summary

The six operators at a glance

Take a = 0b1100 (12) and b = 0b1010 (10).

OperatorNameExampleResultBinary
a & bAND12 & 1080b1000
a | bOR12 | 10140b1110
a ^ bXOR12 ^ 1060b0110
~aNOT~12-13(see below)
a << nleft shift12 << 2480b110000
a >> nright shift12 >> 230b11
python

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.

How negative numbers behave

Why ~x equals -x - 1

~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.

python
text

If you want the inverse limited to a width such as 8 bits, mask the result:

python

Masking to see two's complement

bin(-5) prints -0b101, not the bit pattern. To get the fixed-width form, AND the number with a mask.

python

Right shift keeps the sign

>> is an arithmetic shift: the sign bit is copied in. Negative numbers floor towards negative infinity.

python

There is no separate unsigned shift operator like >>> in Python. Shift counts must be non-negative: 1 << -1 raises ValueError: negative shift count.

Practical bit manipulation patterns

The classic flag operations use one bit per option.

python

Other idioms that come up often:

python

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:

python

Operator precedence and other types

Precedence differs from C

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.

Sets and booleans

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.

python

Bitwise operators accept only integers (and types that define them). 1.5 & 1 raises TypeError.

Frequently Asked Questions

What is the difference between & and and?

& 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.

Why does ~5 give -6?

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.

Is there an unsigned right shift in Python?

No. Mask first, for example (x & 0xFFFFFFFF) >> n, to emulate a 32-bit unsigned shift.

How do I count set bits?

Use n.bit_count() on Python 3.10 or later. On older versions use bin(n).count("1").

Summary

&, |, ^, ~, << 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.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?