{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# BitField Primitive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from byteclasses.print import byteclass_info, byteclass_inspect\n", "from byteclasses.types.primitives.bitfield import BitField, BitField16, BitField32, BitField64, BitPos" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bitfield_types = [BitField, BitField16, BitField32, BitField64]\n", "\n", "my_bfs = []\n", "for i, type_cls in enumerate(bitfield_types):\n", " my_bfs.append(type_cls(data=b\"\\xFF\" * 2**i))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for var in my_bfs:\n", " byteclass_info(var)\n", " byteclass_inspect(var)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bit values can be accesed using several methods." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Using the `get_bit(idx)` or `set_bit(idx, value)` methods" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bf = BitField()\n", "bf.set_bit(0)\n", "bf.set_bit(7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bf.get_bit(0), bf.get_bit(7))\n", "print(bf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. or via instance indexing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bf[1] = True\n", "bf[2] = True\n", "print(bf)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bf[2], bf[3], bf[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bulk Value Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Data assignment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bf.data = b\"\\x00\"\n", "print(bf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Value assignment with `boolean` will set all bits in bitfield to the boolean value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bf.value = True\n", "print(bf)\n", "bf.value = False\n", "print(bf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Value assignment with `dict[int, bool]` will assign the boolean value of given key using the key as the bit index. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bf.value = {0: True, 2: True, 4: True, 6: True}\n", "print(bf)\n", "bf.value = {0: False, 2: False, 4: False, 6: False}\n", "print(bf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Value assignment with `Iterable[bool]` will assign boolean values to bits starting at idx 0 and continuing until the iterable is exhausted." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bf.value = [True, True, True, True]\n", "print(bf)\n", "bf.value = [False, False, False, False]\n", "print(bf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subclassing the `BitField` class allows multi-byte bitfields and named bit positions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class MultiBitField(BitField):\n", " byte_length = 2\n", " first = BitPos(0, bit_width=4)\n", " last = BitPos(15)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mbf = MultiBitField()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "byteclass_info(mbf)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "byteclass_inspect(mbf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Named bits can be used to get or set the bit position specified when instantiating the `BitPos` class member." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mbf.first = True\n", "mbf.last = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(mbf, mbf.first, mbf.last)\n", "print(mbf.flags)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }