Skip to content

Conversation

@khusmann
Copy link
Owner

@khusmann khusmann commented May 4, 2025

I'm not sure if I like this feature, so I'm keeping it off the main branch for now and will consider adding it in later if folks convince me otherwise.

This allows you to re-order bits in a bitfield before it is parsed. So if you have a bitfield like this:

0-4: Lower 4 bits of uint8 field
4-11: Padding
12-15: Upper 4 bits of uint8 field

You can create a bitfield like this:

 class Work(bd.Bitfield):
    a: int = bd.uint_field(8)
    _pad: int = bd.uint8_field(8, default = 0)

    bitfield_config = bd.BitfieldConfig(
        reorder_bits=[*range(12, 16), *range(0, 4)]
     )

The reorder_bits option in the configuration will reorder the bits like this:

12, 13, 14, 15, 0, 1, 2, 3, (rest of bits in stream)

So that field a in the bitfield is parsed all at once.

At first glance this feature seemed nice, but in practice it got a little ugly and error-prone. In general, I think it's a better idea to just read the bitfield as is, (with an a_lower and a_upper field) and then combine them when you convert from bitfield -> domain object in your code (so use the bitfield object similar to how you'd work with an ORM object -- useful for IO, but confined to the edges of your system)

I'm not sure if I like this feature, so I'm keeping it off
the main branch for now and will consider adding it in later.
@khusmann khusmann force-pushed the feat-bit-reorder branch from aba8602 to 7c5bfcc Compare May 4, 2025 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants