12 lines
301 B
Python
Raw Normal View History

2019-05-15 18:36:32 +02:00
def set_bitfield_bit(bitfield, i):
"""
Set the bit in ``bitfield`` at position ``i`` to ``1``.
"""
byte_index = i // 8
bit_index = i % 8
return (
bitfield[:byte_index] +
bytes([bitfield[byte_index] | (1 << bit_index)]) +
bitfield[byte_index + 1:]
2019-05-15 18:36:32 +02:00
)