Bit Field Designer
Lay out named bit fields in a register and pack their values into one word, or decode a word back into its fields. Each field gets its mask and shift, and overlaps or oversized values are flagged.
How to Use
- Choose the register width: 8, 16, 32 or 64 bits.
- List the fields, one per line: a name, the bit range high:low (or a single bit) and a value, e.g.
MODE 30:28 5. Values may be decimal, 0x hex or 0b binary. - In pack mode, read the word in hex, decimal and binary.
- In unpack mode, type a word such as 0xD0FA03E8 to read each field’s value.
- The table gives each field’s mask and the shift-and-mask expression to extract it.
Worked Example
Packing a timer control register. EN = 1 at bit 31 gives 1 << 31 = 0x80000000. MODE = 5 in bits 30:28 gives 5 << 28 = 0x50000000. PRESCALE = 0xFA in bits 27:16 gives 0x00FA0000. COUNT = 1000 = 0x3E8 in bits 15:0 gives 0x000003E8. OR them together: 0xD0FA03E8.
Unpacking it again. MODE is (0xD0FA03E8 >> 28) & 0x7: shifting right 28 leaves 0xD = 1101, and the 3-bit mask keeps 101 = 5. PRESCALE is (word >> 16) & 0xFFF = 0x0FA.
The common mistake: forgetting the mask. (word >> 28) alone gives 0xD = 13, because the EN bit above MODE comes along too. Every field except the top one needs the AND with its mask after the shift.
Show Work
Formulas
Registers, Maps and Headers
Hardware is controlled through registers in which every bit or group of bits means something: an enable flag here, a clock divider there. Microcontroller datasheets describe them as register maps, tables of field names and bit ranges, and vendors now publish the same information in machine-readable form (ARM’s CMSIS-SVD files) so tools can generate the masks and shifts automatically.
The same packing appears everywhere data has to be compact: the first byte of every IPv4 header holds the version and the header length in four bits each, a 16-bit RGB565 pixel packs red, green and blue into 5, 6 and 5 bits, and file systems pack permissions, dates and flags into single words.
About This Calculator
This tool lays out named bit fields in an 8, 16, 32 or 64-bit word, packs their values into the word or unpacks a word into the fields, and lists each field’s mask and the expression to extract it. It flags fields that overlap, fall outside the word or are given values too big for their width, and draws the register with every field labelled.
Everything runs in your browser; nothing is sent anywhere. The field list is plain text, so you can paste it from your own notes and copy it back.
Related calculators: Bitwise Calculator, Bit Manipulation Toolkit, and Endianness Converter.
Frequently Asked Questions
How do I pack values into a register?
Shift each field’s value left by its lowest bit number and OR the results together. MODE = 5 in bits 30:28 contributes 5 << 28 = 0x50000000; with EN = 1 at bit 31, PRESCALE = 0xFA at 27:16 and COUNT = 1000 at 15:0 the word is 0xD0FA03E8.
How do I extract a field from a word?
Shift the word right by the field’s lowest bit, then AND with a mask of the field’s width: (word >> 28) & 0x7 gives MODE. The mask for a w-bit field is (1 << w) − 1.
How do I change one field and keep the others?
Clear the field, then OR in the new value: word = (word & ~(0x7 << 28)) | (newMode << 28). This read-modify-write is how drivers update control registers without disturbing other settings.
What happens if a value is too big for its field?
It would spill into the neighbouring field. A 3-bit field holds 0 to 7, so writing 9 (1001) would set the bit above it. This tool warns and keeps only the low bits, which is what masking the value before shifting does.
Why not use C bit-fields?
C’s struct { unsigned mode : 3; } bit-fields leave the order of the bits, padding and alignment to the compiler, so they cannot be relied on to match a hardware register or a file format. Explicit shifts and masks give the same result on every compiler.
How do I use the Bit Field Designer?
Simply type your numbers and read the result, which refreshes the instant you change something. There is nothing to submit and nothing to wait for.
Is it free? Does it work without internet?
Yes to both. It is free with no sign-up, and once the page has loaded it keeps working even with no internet.
Where does my data go?
Nowhere — every calculation runs on your own device. Nothing you enter is uploaded, logged, or stored.
Common Use Cases
Embedded drivers
Pack EN, MODE, PRESCALE and COUNT into a timer control register: 0xD0FA03E8.
Debugging
Unpack a register value read from hardware into its named fields.
Protocol headers
An IPv4 first byte 0x45 is version 4 (bits 7:4) and header length 5 (bits 3:0).
Colour formats
RGB565: R 15:11, G 10:5, B 4:0; pure orange (31, 32, 0) packs to 0xFC00.
Last updated: