Skip to content

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.

The operators &, |, ^ and ~ are bitwise and, or, xor and not, and << and >> shift bits left and right.

Bits are numbered from 0, the lowest (rightmost in binary), so bit 0 of 0b100 is 0 and bit 2 is 1.

Tags: binary

  • popcount how many bits are 1
  • bit bit i of n, as 0 or 1, where bit 0 is the lowest
  • set_bit n with bit i turned on (set to 1)
  • clear_bit n with bit i turned off (set to 0)
  • rotl treat n as a width-bit number and rotate it left by k
  • rotr treat n as a width-bit number and rotate it right by k
  • byteswap reverse the order of the bytes in a width-bit number, converting between little- and big-endian
  • to_fixed x as a fixed-point integer with int_bits whole bits and frac_bits fractional ones, rounded, shown in hex
  • from_fixed the number a fixed-point integer with int_bits whole bits and frac_bits fractional ones stands for
  • bcd a non-negative integer in binary-coded decimal, where each decimal digit takes one hex digit, so 1234 is 0x1234
  • from_bcd the number a binary-coded decimal integer stands for, the reverse of bcd
1
# to bits shows binary, grouped in fours
2
0xf0 to bits"0b1111_0000"
Example outputCtrl+Enter to run
popcount(n: int) -> int

how many bits are 1

A negative number counts as its two’s complement

1
popcount(0b1011)3
2
popcount(-1)64
Example outputCtrl+Enter to run

See also bit

bit(n: int, i: int) -> int

bit i of n, as 0 or 1, where bit 0 is the lowest

1
bit(0b100, 2)1
Example outputCtrl+Enter to run

See also set_bit, clear_bit, popcount

set_bit(n: int, i: int) -> int

n with bit i turned on (set to 1)

1
set_bit(0b1, 4)0b10001
Example outputCtrl+Enter to run

See also clear_bit, bit

clear_bit(n: int, i: int) -> int

n with bit i turned off (set to 0)

1
clear_bit(0xff, 0)0xfe
Example outputCtrl+Enter to run

See also set_bit, bit

rotl(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

1
rotl(0x81, 1, 8)0x3
2
rotl(0x80000000, 1, 32)0x1
Example outputCtrl+Enter to run

See 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

1
rotr(0x81, 1, 8)0xc0
Example outputCtrl+Enter to run

See also rotl

byteswap(n: int, width: int) -> int

reverse the order of the bytes in a width-bit number, converting between little- and big-endian

1
byteswap(0x1234, 16)0x3412
2
byteswap(0x12345678, 32)0x78563412
Example outputCtrl+Enter to run

See also rotl

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

1
to_fixed(1.5, 8, 8)0x0180
2
to_fixed(-0.25, 4, 4)0xfc
Example outputCtrl+Enter to run

See also 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

1
from_fixed(0x0180, 8, 8)1.5
2
from_fixed(0xf0, 4, 4)-1
Example outputCtrl+Enter to run

See 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

1
1234 to bcd0x1234
2
bcd(59)0x59
Example outputCtrl+Enter to run

See also from_bcd

from_bcd(n: int) -> int

the number a binary-coded decimal integer stands for, the reverse of bcd

1
from_bcd(0x1234)1234
Example outputCtrl+Enter to run

See also bcd

1
# xor: bits that differ
2
0b1010 ^ 0b01100b1100
3
# how many bits are 1
4
popcount(0xff)8
5
# rotate within 8 bits
6
rotr(1, 1, 8)128
7
# swap byte order
8
byteswap(0x1234, 16)0x3412
9
# clear a bit
10
clear_bit(0xff, 0)0xfe
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.