BitField Primitive¶
[1]:
from byteclasses.print import byteclass_info, byteclass_inspect
from byteclasses.types.primitives.bitfield import BitField, BitField16, BitField32, BitField64, BitPos
[2]:
bitfield_types = [BitField, BitField16, BitField32, BitField64]
my_bfs = []
for i, type_cls in enumerate(bitfield_types):
my_bfs.append(type_cls(data=b"\xFF" * 2**i))
[3]:
for var in my_bfs:
byteclass_info(var)
byteclass_inspect(var)
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ BitField │ │ is_byteclass() │ True │ │ is_collection_instance() │ False │ │ is_primitive_instance() │ True │ │ mro │ BitField -> _Primitive -> object │ │ len() │ 1 │ │ str() │ BitField(11111111, flags={}) │ │ repr() │ BitField(data=b'\xff') │ │ .data │ b'\xff' │ │ .value │ (True, True, True, True, True, True, True, True) │ └──────────────────────────┴──────────────────────────────────────────────────┘
╭────────────────── Byteclass Inspect ───────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |ff │ ╰────────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ BitField(11111111, flags={}) │ └──────────────────────────────┘
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ BitField16 │ │ is_byteclass() │ True │ │ is_collection_instance() │ False │ │ is_primitive_instance() │ True │ │ mro │ BitField16 -> BitField -> _Primitive -> object │ │ len() │ 2 │ │ str() │ BitField16(1111111111111111, flags={}) │ │ repr() │ BitField16(data=b'\xff\xff') │ │ .data │ b'\xff\xff' │ │ .value │ (True, True, True, True, True, True, True, True, True, True, True, True, True, True, │ │ │ True, True) │ └──────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────┘
╭────────────────── Byteclass Inspect ───────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |ff ff │ ╰────────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ BitField16(1111111111111111, flags={}) │ └────────────────────────────────────────┘
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ BitField32 │ │ is_byteclass() │ True │ │ is_collection_instance() │ False │ │ is_primitive_instance() │ True │ │ mro │ BitField32 -> BitField -> _Primitive -> object │ │ len() │ 4 │ │ str() │ BitField32(11111111111111111111111111111111, flags={}) │ │ repr() │ BitField32(data=b'\xff\xff\xff\xff') │ │ .data │ b'\xff\xff\xff\xff' │ │ .value │ (True, True, True, True, True, True, True, True, True, True, True, True, True, True, │ │ │ True, True, True, True, True, True, True, True, True, True, True, True, True, True, │ │ │ True, True, True, True) │ └──────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────┘
╭────────────────── Byteclass Inspect ───────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |ff ff ff ff │ ╰────────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ BitField32(11111111111111111111111111111111, flags={}) │ └────────────────────────────────────────────────────────┘
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ BitField64 │ │ is_byteclass() │ True │ │ is_collection_instance() │ False │ │ is_primitive_instance() │ True │ │ mro │ BitField64 -> BitField -> _Primitive -> object │ │ len() │ 8 │ │ str() │ BitField64(1111111111111111111111111111111111111111111111111111111111111111, │ │ │ flags={}) │ │ repr() │ BitField64(data=b'\xff\xff\xff\xff\xff\xff\xff\xff') │ │ .data │ b'\xff\xff\xff\xff\xff\xff\xff\xff' │ │ .value │ (True, True, True, True, True, True, True, True, True, True, True, True, True, True, │ │ │ True, True, True, True, True, True, True, True, True, True, True, True, True, True, │ │ │ True, True, True, True, True, True, True, True, True, True, True, True, True, True, │ │ │ True, True, True, True, True, True, True, True, True, True, True, True, True, True, │ │ │ True, True, True, True, True, True, True, True) │ └──────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────┘
╭────────────────── Byteclass Inspect ───────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |ff ff ff ff ff ff ff ff │ ╰────────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ BitField64(1111111111111111111111111111111111111111111111111111111111111111, flags={}) │ └────────────────────────────────────────────────────────────────────────────────────────┘
Bit values can be accesed using several methods.
Using the
get_bit(idx)
orset_bit(idx, value)
methods
[4]:
bf = BitField()
bf.set_bit(0)
bf.set_bit(7)
[5]:
print(bf.get_bit(0), bf.get_bit(7))
print(bf)
True True
BitField(10000001, flags={})
or via instance indexing
[6]:
bf[1] = True
bf[2] = True
print(bf)
BitField(11100001, flags={})
[7]:
print(bf[2], bf[3], bf[-1])
True False True
Bulk Value Assignment¶
Data assignment
[8]:
bf.data = b"\x00"
print(bf)
BitField(00000000, flags={})
Value assignment with
boolean
will set all bits in bitfield to the boolean value.
[9]:
bf.value = True
print(bf)
bf.value = False
print(bf)
BitField(11111111, flags={})
BitField(00000000, flags={})
Value assignment with
dict[int, bool]
will assign the boolean value of given key using the key as the bit index.
[10]:
bf.value = {0: True, 2: True, 4: True, 6: True}
print(bf)
bf.value = {0: False, 2: False, 4: False, 6: False}
print(bf)
BitField(10101010, flags={})
BitField(00000000, flags={})
Value assignment with
Iterable[bool]
will assign boolean values to bits starting at idx 0 and continuing until the iterable is exhausted.
[11]:
bf.value = [True, True, True, True]
print(bf)
bf.value = [False, False, False, False]
print(bf)
BitField(11110000, flags={})
BitField(00000000, flags={})
Subclassing the BitField
class allows multi-byte bitfields and named bit positions.
[12]:
class MultiBitField(BitField):
byte_length = 2
first = BitPos(0, bit_width=4)
last = BitPos(15)
[13]:
mbf = MultiBitField()
[14]:
byteclass_info(mbf)
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ MultiBitField │ │ is_byteclass() │ True │ │ is_collection_instance() │ False │ │ is_primitive_instance() │ True │ │ mro │ MultiBitField -> BitField -> _Primitive -> object │ │ len() │ 2 │ │ str() │ MultiBitField(0000000000000000, flags={'first': 0, 'last': False}) │ │ repr() │ MultiBitField(data=b'\x00\x00') │ │ .data │ b'\x00\x00' │ │ .value │ (False, False, False, False, False, False, False, False, False, False, False, False, │ │ │ False, False, False, False) │ └──────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────┘
[15]:
byteclass_inspect(mbf)
╭────────────────── Byteclass Inspect ───────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00 00 │ ╰────────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ MultiBitField(0000000000000000, flags={'first': 0, 'last': False}) │ └────────────────────────────────────────────────────────────────────┘
Named bits can be used to get or set the bit position specified when instantiating the BitPos
class member.
[16]:
mbf.first = True
mbf.last = True
[17]:
print(mbf, mbf.first, mbf.last)
print(mbf.flags)
MultiBitField(1000000000000001, flags={'first': 1, 'last': True}) 1 True
{'first': 1, 'last': True}