Checking a packed integer with two fixed-width fields
Store two small unsigned values in one integer by reserving bits for the lower field, scaling the upper field by a power of two and adding.
Why a field width has to be declared first
Two small numbers can share one integer only if the reader knows where one ends and the other begins. That boundary is a width in bits. If the lower field gets four bits, it can hold values from 0 through 15, and the upper field starts at bit position four. The upper value is multiplied by 2 to the fourth, which is 16, so its contribution lands entirely above the lower positions. Without a stated width, the integer 89 could be read as 5 and 9, as 8 and 1, or as one plain number. This worksheet fixes the width at four bits and checks the arithmetic of the packed result.
Packing 5 and 9 step by step
Take an upper value of 5 and a lower value of 9. Both fit in four bits, since the largest four-bit value is 15. The upper contribution is 5 times 16, and the basic calculator returns 80. Adding the lower value gives 80 plus 9, which is 89. Because 80 is a multiple of 16, its four low bits are all zero, so adding 9 only fills those positions and carries nothing. In binary, 5 is 0101 and 9 is 1001, so the packed value is 0101 1001. Keeping the contributions apart is the whole point of multiplying by 16 before adding.
Reading the packed value in hexadecimal
Hexadecimal makes four-bit fields easy to see, because one hexadecimal digit stands for exactly four bits. The Integer Base Converter with value 89 in base 10 and target base 16 returns 59. The left digit is the upper field, 5, and the right digit is the lower field, 9. A second packing shows the same effect: an upper value of 6 and a lower value of 2 give 6 times 16 plus 2, which is 98, and the converter shows 62 in base 16. This only holds when each field really occupies one four-bit group. A field with a different width will not line up with the hex digits.
Unpacking with division and subtraction
To recover the fields, divide the packed integer by 16. For 89, the basic calculator returns 5.5625, so the whole-number part is 5, which is the upper value. Multiplying 5 by 16 gives 80, and subtracting from 89 leaves 9, the lower value. Programmers usually write this as a right shift by four bits for the upper field and a bitwise AND with 15 for the lower one, and the arithmetic version produces the same fields for nonnegative inputs. The lower mask 15 is 2 to the fourth minus 1, and it keeps exactly four bits. The upper field has no mask in this example because nothing sits above it.
What happens when the lower value is too large
Suppose the lower value is 16 while the field has only four bits. Then 5 times 16 plus 16 equals 96, which is 60 in hexadecimal. A reader who expects an upper field of 5 and a lower field of 16 will instead decode upper 6 and lower 0. The excess carried into the upper positions and changed it. The sum looks like an ordinary integer, so nothing signals that the layout was violated. Check each field against its range before packing. For a four-bit lower field the range is 0 through 15, and the value 16 needs a fifth bit, which would have to push the upper field higher.
Choosing a wider layout
The same method scales with the field width. With an eight-bit lower field, the multiplier is 2 to the eighth, which is 256. An upper value of 200 and a lower value of 17 pack as 200 times 256 plus 17, which is 51,217. This is the same arithmetic with a larger factor. Both fields use full bytes, so the packed result needs 16 bits and its largest value is 65,535. If you pack a four-bit upper field and a four-bit lower field, the maximum is 15 times 16 plus 15, which is 255. Plan the total width up front so that the packed value fits the storage type, whether that is a byte, a 16-bit word or a database column.
Where packed fields are used
Packing small values is common in file formats, network headers and hardware registers, where space is limited and each bit has a defined meaning. A date in the old MS-DOS FAT format, for example, stores year, month and day together in a 16-bit word. Display colors are often packed into one integer, with channels sharing the bits. In every case a specification states the widths and the order of the fields. This worksheet does not choose any of those layouts. It checks the simple arithmetic that underlies them, using one upper and one lower field, so that you can test your reasoning on small numbers before applying it to a real format.
Which field goes on top
The order of the fields is part of the layout. Packing the same two numbers with 9 on top and 5 below gives 9 times 16 plus 5, which is 149, and the base converter shows 95 in hexadecimal. The digits swap places, and the integer is quite different from 89. Putting the more significant field on top also makes the packed integers sort the same way as the pairs: comparing upper values first and lower values second gives the same order as comparing the integers. Here 5 and 9 pack to 89, and 6 and 2 pack to 98, so the pair with the larger upper value is also the larger integer, even though its lower value is smaller.
What the base converter does and does not do
The Integer Base Converter parses a digit string and writes the same integer in another base. It reports the digits of a number, and it has no knowledge of the layout you have in mind. Converting 89 to base 16 gives 59, and it would do the same for a value that was packed incorrectly. The converter therefore cannot tell you that the lower field overflowed. That check belongs to you: confirm each field fits its declared width, compute the packed value, and then use the conversion to inspect the digits. The arithmetic examples here assume the two fields are disjoint, and the layout is an external rule. Treat any packed value that you receive from another system as unverified until you know its field widths and their order.
Try your own values with the Unsigned bit field packing calculator.
Reproduce the checks
Upper field contribution
Open the Basic calculator and enter:
- First number
5- Operation
multiply- Second number
16
Expected output:
Result
80Four lower bits supply the factor sixteen.
Packed integer
Open the Basic calculator and enter:
- First number
80- Operation
add- Second number
9
Expected output:
Result
89The lower contribution occupies the reserved positions.
Packed hexadecimal value
Open the Integer Base Converter and enter:
- Value
89- Source base
10- Target base
16
Expected output:
Value
59Each hexadecimal digit corresponds to one four-bit field here.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.
Sources
- Primary definition and convention reference ↗ · 2026-10-08