Skip to content

komm.to_binary

Converts a single integer to its bit representation. Unlike int_to_bits, it works with Python integers of arbitrary size.

Parameters:

  • integer (int)

    The input integer. Must be non-negative.

  • width (int | None)

    The number of bits of the representation. The default value is the number of bits of the input integer.

  • bit_order (Literal['LSB-first', 'MSB-first'])

    Bit order convention. Must be either "LSB-first" (least significant bit in the first position) or "MSB-first" (most significant bit in the first position). The default value is "LSB-first".

Returns:

  • bits (list[int])

    The bit representation of the input integer, as a list of bits.

Examples:

>>> komm.to_binary(16, width=6)
[0, 0, 0, 0, 1, 0]
>>> komm.to_binary(16)
[0, 0, 0, 0, 1]
>>> komm.to_binary(16, bit_order="MSB-first")
[1, 0, 0, 0, 0]
>>> komm.from_binary(komm.to_binary(2**100))
1267650600228229401496703205376