Bit Field Packer Calculator

Enter each value and its bit width to pack them into one integer.

How it is calculated

Up to three non-negative whole numbers are placed side by side in one integer, with field 1 in the highest bits and field 3 in the lowest: packed = (v1 << (w2 + w3)) | (v2 << w3) | v3. Each w is the number of bits reserved for that field, and each value must be smaller than 2w, otherwise there is no result. Field 3 is optional: if left empty, both its value and its width count as 0. Decimals are cut off to whole numbers, and negative values or widths give no result. The sum of the widths must be between 1 and 63 bits. The packed result is shown in decimal, in hexadecimal and in binary padded to the total width.

Worked example

This is the RGB565 layout for a 16-bit pixel: red 21 in 5 bits, green 42 in 6 bits, blue 10 in 5 bits. Total width: 5 + 6 + 5 = 16 bits. Red moves left by 6 + 5 = 11 bits (21 × 2048 = 43,008). Green moves left by 5 bits (42 × 32 = 1,344). Blue stays as 10. The sum is 43,008 + 1,344 + 10 = 44,362. The tool shows the packed value in three forms, one per line (decimal, hexadecimal, binary): 44,362 0xAD4A 0b1010110101001010

Try this example →

Common mistakes

  • Entering a value too large for its width. A 5-bit field holds 0 to 31, so 32 gives no result; widen the field or check the value.
  • Expecting field 1 to be in the lowest bits. Field 1 is the most significant part, so the order of the fields on the screen is the order from the high bit down.
  • Typing the width as the maximum value (31 instead of 5). The width counts bits, not the largest number that fits.
  • Entering decimals or negative numbers as signed values. Fractions are truncated and negatives are rejected, so two's-complement fields must be converted to unsigned values first.
  • Forgetting the 63-bit limit when packing into a 64-bit register. The total width is capped at 63, and unused fields should be left at width 0 with value 0.

About this calculator

A bit field packs several small integer values into contiguous groups of bits within one larger integer, so that a single 32-bit or 64-bit word can hold, say, a 5-bit value, an 8-bit value, and a 3-bit value all at once instead of using a separate variable — and separate memory — for each. This calculator takes up to three values with their bit widths, shifts each into its position, ORs them together, and shows the combined result in decimal, hexadecimal, and binary.

Bit-field packing is everywhere in low-level programming: hardware peripheral configuration registers on microcontrollers assign specific bit ranges to specific settings, network protocol headers (like IPv4’s version/IHL byte or TCP flags) pack multiple fields into a few bytes to minimize overhead, and graphics formats like RGB565 pack red, green, and blue channels into a single 16-bit pixel value. Embedded systems and driver developers use exactly this kind of packing to read and write hardware registers correctly.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    When you build a register value from a datasheet, work from the top. Field 1 here sits in the highest bits, so list the datasheet's fields from the most significant bit down and use a width of 0 for any gap you leave unused at the very end. Also check any reserved bits in the middle of the register: this tool has no place for them, so add them as a field with value 0. What register or protocol header are you packing?

Popular calculators

All Calculators