&

Bit-level AND — masks, flags, and (with sets) intersection. Not the logical `and`.

Bitwise operatorPython 1.0+Live demo
Common call
flags & READ
Returns
int with only the shared bits; set intersection
Replaces
12 & 10 == 8 (0b1100 & 0b1010 = 0b1000)
Watch out
binds TIGHTER than ==: x & 1 == 0 parses as x & (1 == 0)
aaLeft operand.type: int | set · required & bbRight operand.type: int | set · required
int | set

Demo

Live evaluation
Try:
Inputs
aintleft operand
bintright operand
Output
12 & 10
8

12 & 10: 0b1100 & 0b1010 keeps only the bit both share — 0b1000 = 8. Masking with 1 isolates the lowest bit (a fast odd/even test).

Operands

NameTypeRequiredDescription
aint | setyesLeft operand.
bint | setyesRight operand.

Return value

int | setInts: a bit is set only where BOTH operands have it. Sets: the intersection.

Common patterns

Flag testing
The classic permissions check.
READ, WRITE = 0b01, 0b10
if perms & WRITE:
    save()
Even/odd via the low bit
n & 1 is the branch-free parity check.
is_odd = n & 1
Set intersection
Common elements of two sets.
both = tags_a & tags_b

Examples

1. Bit AND
12 & 10
Returns
8
2. Low-bit mask
7 & 1
Returns
1
3. Set intersection
{1, 2} & {2, 3}
Returns
{2}

Pitfalls

1. Precedence vs ==
& binds tighter than comparisons — the C-programmer trap reversed.
Wrong parse
x & 1 == 0
x & (1 == 0) → x & False → 0
Fix
(x & 1) == 0
the intended parity test
2. & is not `and`
No short-circuit, no truthiness — pure bit math.
Wrong tool
if is_valid & save():   # both always run
bitwise on bools, no short-circuit
Fix
if is_valid and save():
logical, short-circuits

When to use

Use it
  • Flag masks and permission bits
  • Parity and low-bit tricks
  • Set intersection
Reach for something else
  • Logical conjunction → and
  • Element-wise on arrays → numpy (where & IS the convention)

Notes

Complexity
O(bits); Python ints are arbitrary precision
Return
int (or set); operands untouched
CPython impl
Objects/longobject.c :: long_and → __and__
Memory
No allocation for small ints
Thread-safe
Yes — pure computation

FAQ

They overload the BITWISE operators element-wise because and/or cannot be overloaded (they are control flow). Hence the mandatory parentheses in (df.a > 0) & (df.b < 5).

History

1.0
Core operator from the beginning.