Binary Bitfield Calculator
Encode multiple named fields into one packed binary or hexadecimal bitfield, or decode a packed value into individual fields with custom widths, bit ranges, offsets and masks.
| Field | Width | Bit Range | Offset | Binary | Hex | Decimal | Mask |
|---|---|---|---|---|---|---|---|
| Define fields and calculate to view the bitfield breakdown. | |||||||
What Is a Binary Bitfield?
A bitfield is a group of smaller values packed into specific bit positions inside a larger binary word. Each field occupies a defined number of bits and normally represents a flag, status code, mode, counter, identifier or other compact numeric value.
Bitfields are common in hardware registers, embedded systems, network protocols, file formats and binary data structures because several small values can share one integer efficiently.
Bitfield Encoding
Encoding combines multiple fields into one packed value. The calculator places the first field at the most-significant side and subsequent fields immediately after it.
Mode = 5, width = 3 bits
Enable = 1, width = 1 bit
Count = 2, width = 4 bits
Binary:
101 | 1 | 0010
Packed:
10110010
Hex:
B2
Bitfield Decoding
Decoding performs the reverse process. You provide a packed binary or hexadecimal value and define how many bits belong to each field.
10110010
Field widths:
3 | 1 | 4
Decoded:
101 = 5
1 = 1
0010 = 2
MSB-to-LSB Field Ordering
This calculator treats the first field definition as the most-significant field and the final field as the least-significant field.
A = 3 bits
B = 2 bits
C = 3 bits
Packed layout:
AAA BB CCC
Bit positions:
7 … 5 | 4 … 3 | 2 … 0
Bit Offset and Bit Range
The offset of a field is the number of bits between its least-significant bit and bit position 0 of the complete packed value.
For example, in an 8-bit layout with fields of widths 3, 1 and 4, the field ranges are:
| Field | Width | Bit Range | Offset |
|---|---|---|---|
| Mode | 3 | 7:5 | 5 |
| Enable | 1 | 4 | 4 |
| Count | 4 | 3:0 | 0 |
What Is a Bitfield Mask?
A bit mask contains ones at the positions occupied by a field and zeros elsewhere. It can be used with bitwise operations to isolate or update a field.
bits 3:0
Binary mask:
00001111
Hex mask:
0F
A field occupying bits 7:5 would instead use the binary mask 11100000.
Field Value Limits
An unsigned field of width n can represent values from zero through 2 raised to n minus one.
| Width | Unsigned Range | Binary Maximum |
|---|---|---|
| 1 bit | 0 to 1 | 1 |
| 2 bits | 0 to 3 | 11 |
| 3 bits | 0 to 7 | 111 |
| 4 bits | 0 to 15 | 1111 |
| 8 bits | 0 to 255 | 11111111 |
| 16 bits | 0 to 65535 | 16 ones |
Bitfield Example: Hardware Register
A simple 8-bit control register might divide its bits as follows:
| Field | Bits | Purpose |
|---|---|---|
| MODE | 7:6 | Operating mode |
| ENABLE | 5 | Enable flag |
| CHANNEL | 4:2 | Channel number |
| STATUS | 1:0 | Status code |
Packing these values into one byte makes the register compact while still allowing every field to be extracted independently.
Where Bitfields Are Used
Hardware Registers
Individual control and status fields are commonly packed into processor and peripheral registers.
Network Protocols
Protocol headers frequently assign exact bit ranges to flags, lengths, types and control values.
Embedded Systems
Bitfields reduce storage and allow several configuration values to fit inside a small integer.
Binary File Formats
Packed metadata fields can store multiple attributes without allocating a full byte to every property.
Important Bitfield Notes
Every field uses an unsigned value.
The entered value must fit within its specified field width.
For an n-bit unsigned field, the maximum valid value is 2^n – 1.
The total width is the sum of all field widths.
Binary decoding requires the packed input width to match the total defined field width.
Hexadecimal input is left-padded with zeros when necessary to match the field layout.
Bit numbering uses bit 0 as the least-significant bit of the entire packed value.