Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bitwise Operations

All bitwise operations follow Python’s two’s complement semantics for negative numbers.

OperatorDescription
a & bBitwise AND
a | bBitwise OR
a ^ bBitwise XOR
~aBitwise NOT: $~x = -(x + 1)$

Each accepts both BInt and Int as the right operand. In-place variants (&=, |=, ^=) are also available.

var a = BInt(0b1100)
var b = BInt(0b1010)
print(a & b)   # 8   (0b1000)
print(a | b)   # 14  (0b1110)
print(a ^ b)   # 6   (0b0110)
print(~a)      # -13

# Negative numbers use two's complement:
print(BInt(-1) & BInt(255))  # 255