Extracting a Packed Bit Field
To read a field inside an unsigned packed value, shift its lowest position down to zero and isolate the bits in its width.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a three-bit field
In 11010110, positions two through four hold 101 when shown most significant first. Shifting right two and masking with 111 gives numeric five. Bitwise and performs that final selection.
Check ordering conventions
Position zero typically denotes the least significant bit in this formula. A file format may number drawn positions differently, and byte order controls how bytes were assembled before the bit operation.[1]
Shift the first field position to zero before masking
In byte 11011010, bits four through two form 110 under zero-based numbering from the right. Shifting right two gives 00110110. Masking the low three bits then gives six. See bitwise AND selection.
Raw byte 11011010
Shift right by 2 00110110
Low-three-bit mask 00000111
Result 00000110
Validate width and decide if the result is signed
A field's zero-based offset plus its width must fit inside the source word. Extracting bits produces a pattern, and signed meaning requires a separate interpretation using the field's width. Test a field spanning the high bit.