math.bits
the individual bits of an integer: count, test, set, clear, rotate and swap them, plus fixed-point and binary-coded decimal
The individual bits of an integer, and the number formats built on them: fixed point, which keeps a fixed number of bits after the point, and binary-coded decimal, which spends four bits on each decimal digit.
writing
Section titled “writing”The operators &, |, ^ and ~ are bitwise and, or, xor and not, and << and >> shift bits left and right.
good to know
Section titled “good to know”Bits are numbered from 0, the lowest (rightmost in binary), so bit 0 of 0b100 is 0 and bit 2 is 1.
Tags: binary
popcounthow many bits are 1bitbit i of n, as 0 or 1, where bit 0 is the lowestset_bitn with bit i turned on (set to 1)clear_bitn with bit i turned off (set to 0)rotltreat n as a width-bit number and rotate it left by krotrtreat n as a width-bit number and rotate it right by kbyteswapreverse the order of the bytes in a width-bit number, converting between little- and big-endianto_fixedx as a fixed-point integer with int_bits whole bits and frac_bits fractional ones, rounded, shown in hexfrom_fixedthe number a fixed-point integer with int_bits whole bits and frac_bits fractional ones stands forbcda non-negative integer in binary-coded decimal, where each decimal digit takes one hex digit, so 1234 is 0x1234from_bcdthe number a binary-coded decimal integer stands for, the reverse of bcd
syntax
Section titled “syntax”1# to bits shows binary, grouped in fours20xf0 to bits"0b1111_0000"Functions
Section titled “Functions”popcount
Section titled “popcount”popcount(n: int) -> int
how many bits are 1
A negative number counts as its two’s complement
1popcount(0b1011)32popcount(-1)64See also bit
bit(n: int, i: int) -> int
bit i of n, as 0 or 1, where bit 0 is the lowest
1bit(0b100, 2)1See also set_bit, clear_bit, popcount
set_bit
Section titled “set_bit”set_bit(n: int, i: int) -> int
n with bit i turned on (set to 1)
1set_bit(0b1, 4)0b10001clear_bit
Section titled “clear_bit”clear_bit(n: int, i: int) -> int
n with bit i turned off (set to 0)
1clear_bit(0xff, 0)0xferotl(n: int, k: int, width: int) -> int
treat n as a width-bit number and rotate it left by k
Bits pushed off the top come back in at the bottom, where << would drop them
1rotl(0x81, 1, 8)0x32rotl(0x80000000, 1, 32)0x1See also rotr
rotr(n: int, k: int, width: int) -> int
treat n as a width-bit number and rotate it right by k
Bits pushed off the bottom come back in at the top, where >> would drop them
1rotr(0x81, 1, 8)0xc0See also rotl
byteswap
Section titled “byteswap”byteswap(n: int, width: int) -> int
reverse the order of the bytes in a width-bit number, converting between little- and big-endian
1byteswap(0x1234, 16)0x34122byteswap(0x12345678, 32)0x78563412See also rotl
to_fixed
Section titled “to_fixed”to_fixed(x: num, int_bits: int, frac_bits: int) -> int
x as a fixed-point integer with int_bits whole bits and frac_bits fractional ones, rounded, shown in hex
The whole bits include the sign, so to_fixed(1.5, 8, 8) is the 16-bit number 0x0180
1to_fixed(1.5, 8, 8)0x01802to_fixed(-0.25, 4, 4)0xfcSee also from_fixed
from_fixed
Section titled “from_fixed”from_fixed(n: int, int_bits: int, frac_bits: int) -> float
the number a fixed-point integer with int_bits whole bits and frac_bits fractional ones stands for
1from_fixed(0x0180, 8, 8)1.52from_fixed(0xf0, 4, 4)-1See also to_fixed
bcd(n: int) -> int
a non-negative integer in binary-coded decimal, where each decimal digit takes one hex digit, so 1234 is 0x1234
11234 to bcd0x12342bcd(59)0x59See also from_bcd
from_bcd
Section titled “from_bcd”from_bcd(n: int) -> int
the number a binary-coded decimal integer stands for, the reverse of bcd
1from_bcd(0x1234)1234See also bcd
More examples
Section titled “More examples”1# xor: bits that differ20b1010 ^ 0b01100b11003# how many bits are 14popcount(0xff)85# rotate within 8 bits6rotr(1, 1, 8)1287# swap byte order8byteswap(0x1234, 16)0x34129# clear a bit10clear_bit(0xff, 0)0xfeExample results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.