experimental.bitarray
Support functions to allow using a bytearray as a bit array.
Micropython doesn’t appear to have a native bitarray implementation, so this module serves as a loose framework on top of the bytearray object to allow easier bit-level access.
- experimental.bitarray.get_bit(arr, index)
Get the value of the bit at the nth position in a bytearray
Bytes are stored most significant bit first, so the 8th bit of [1] comes immediately after the first bit of [0]: [ B0b7 B0b6 B0b5 B0b4 B0b3 B0b2 B0b1 B0b0 B1b7 B1b6 … ]
- Parameters
arr – The bytearray to operate on
index – The bit index to retrieve
- Returns
0 or 1, depending on the state at position index
- experimental.bitarray.make_bit_array(length)
Create a bit array that contains at least length bits
The resulting byte array will have a length rounded up to the next byte if length is not divisible by 8
- Parameters
length – The number of bits in the array
- Returns
A bytearray containing at least @length bits
- experimental.bitarray.set_all_bits(arr, value=0)
Set all bits in the array to the same value
- Parameters
arr – The bytearray to reset
value – A truthy value indicating whether all bits should be set to 0 or 1
- experimental.bitarray.set_bit(arr, index, value)
Set the bit at the nth position in a bytearray
Bytes are stored most significant bit first, so the 8th bit of [1] comes immediately after the first bit of [0]: [ B0b7 B0b6 B0b5 B0b4 B0b3 B0b2 B0b1 B0b0 B1b7 B1b6 … ]
- Parameters
arr – The bytearray to operate on
index – The bit position within the array
value – A truthy value indicating whether the bit should be set to 1 or 0