Skip to content

ADR 0062: Bitwise operators

Status: Final Type: Feature Created: 2026-10-13 Amends: 0002 (the modes table gains integer <<) Amended-By: 0075 (an executable's int grows under << as under *) Related-ADRs: 0002 (the overflow rule << follows), 0020 (the other operator whose operand's sign decides what CPython does), 0007 (the ValueError a negative count raises). Provenance: the Shed Skin census (notes/measurements.md, 2026-10-13), where bitwise operators stop 18 programs.

Context

&, |, ^, ~, << and >> were refused as operators not lowered yet, in 18 of the census's 55 programs: hashes, bit sets, image codecs, a Brainfuck interpreter's & 255. Every backend has the instructions. The spec has no operator section, though, and three questions had answers that are not CPython's for free, because CPython's integers have no width:

  • ~x is -x - 1 in CPython. On an unsigned type that value does not exist: ~UInt64(0) is -1 under postyp and 2**64 - 1 in a machine complement.
  • x << n can leave the type, as x * 2**n can: Int64(1) << 63 is one past Int64's largest value.
  • A shift's count is a different kind of operand from its value. If the result were the promotion of both, Int8 << Int64 would silently widen.

Decision

&, | and ^ promote their operands as + does; a Bool with a Bool stays a Bool, as in CPython (True & False is False). On two's-complement values they are CPython's on the same values, so they never overflow and need no check. A signed operand with an unsigned one of the same width or wider is refused, because no integer type holds every value of both and promote would change one of them before the operation (UInt64(2**63) | 0 would become negative).

~x is lowered on a signed type, as x ^ -1, which is CPython's -x - 1 and never overflows (~MIN is MAX). On an unsigned type it is refused, naming x ^ MAX for the complemented bits. On a Bool it is refused too, since CPython deprecates it there (~True is -2).

A shift has the left operand's type, whatever the count's type, as in numpy and Go. A Bool shifted is an int, as in CPython. The count is CPython's:

  • a negative count raises ValueError("negative shift count") in both build modes. It is a raise, so it can be caught. The raiser walk marks a shift as a raiser unless its count folds to a non-negative constant;
  • a count at or past the width gives what CPython's unbounded integer gives. x >> n is 0 or -1; the instruction is emitted with the count clamped to width - 1, where it is defined on every backend;
  • a constant count below the width needs none of this, and emits one instruction.

<< overflows as * does, since x << n is x * 2**n. An executable's int grows past 64 bits under it, in both modes (0075). The 64-bit int of a library, an extension module or a --no-grow-ints executable follows 0002. A debug build checks that the value shifted back is the value shifted, and that a count past the width shifts only a zero, and traps with integer << overflowed. A release build wraps at the width, so a count past it gives 0. A fixed-width type wraps at its width in both modes.

The augmented forms (&=, |=, ^=, <<=, >>=) are the operators on a name, a field or a subscript, by the path every augmented assignment takes.

The IR gains five binary operators, BITAND, BITOR, BITXOR, SHL and SHR, whose operands have the result's type. SHL wraps and SHR is arithmetic on a signed type and logical on an unsigned one. The frontend guarantees a count in [0, width). The C backend shifts a signed value left as its unsigned counterpart, since a signed << that overflows is undefined behaviour in C.

Consequences

  • Programs written against unsigned types use x ^ MAX where they would write ~x. That is the one place a program reads differently compiled than it would under a machine-integer language.
  • &, | and ^ on sets and dicts are not lowered by this decision. A set or a dict operand gets the container's own refusal.
  • ADR 0002's table gains the << row.

Alternatives considered

  • ~ as the machine complement on an unsigned type. It matches C, numpy and Go, and differs from postyp, so a program would print different values interpreted and compiled.
  • << always trapping, or always wrapping. Either one makes x << n and x * 2**n behave differently in one of the two modes.
  • A shift promoting both operands. Int8 << Int64 would become Int64, which reverses the reason a program declares a narrow type.