Compress two or more numbers into one byte
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In computer science, compressing multiple numbers into a single byte is a practical technique for embedded systems, network protocols, game development, and any context where memory is scarce. A byte consists of 8 bits, giving possible values. By dividing those 8 bits into separate fields, each field can represent a different number. This article explains the technique of bit packing, walks through examples with packing and unpacking, and covers the tradeoffs involved.
How Bit Packing Works
Bit packing divides a byte into non-overlapping fields. Each field is a contiguous group of bits dedicated to one number. The total bits allocated across all fields must not exceed 8.
The range of values a field can hold depends on its bit width:
| Bits | Range | Number of Values |
| 1 | to | 2 |
| 2 | to | 4 |
| 3 | to | 8 |
| 4 | to | 16 |
| 5 | to | 32 |
| 6 | to | 64 |
In general, bits can represent values from to .
Example: Packing Two Numbers
Suppose you need to store two numbers:
- Number A: range 0 to 3 (requires 2 bits)
- Number B: range 0 to 15 (requires 4 bits)
Total: 6 bits used, 2 bits unused.
Packing Layout
The high 2 bits hold A, the next 4 bits hold B, and the lowest 2 bits are unused (or available for a third field).
Packing Code
To pack A and B into a single byte:
For example, if A = 2 (binary 10) and B = 9 (binary 1001):
The packed byte is 0xA4 (decimal 164).
Unpacking Code
To extract the numbers back:
Example: Packing Three Numbers
You can fit three numbers if their total bit widths do not exceed 8:
- X: range 0 to 7 (3 bits)
- Y: range 0 to 3 (2 bits)
- Z: range 0 to 7 (3 bits)
Layout:
Packing:
Unpacking:
Summary Table
| Field | Bits | Range | Mask | Extract |
| A | 2 | to | 0x03 | |
| B | 4 | to | 0x0F | |
| Unused | 2 | - | - | - |
Considerations and Tradeoffs
Overflow Risk
If a value exceeds the allocated bits, the extra high bits are silently lost. For example, storing the value 5 (binary 101) in a 2-bit field produces 01 (value 1). Always validate inputs before packing.
No Precision Loss for Integers
Unlike floating-point compression, integer bit packing is lossless within the declared range. The value you pack is exactly the value you unpack.
Increased Code Complexity
Bit manipulation adds complexity to your code. Every pack and unpack site needs to agree on the field layout. Consider defining constants or using a struct/bitfield abstraction in your language.
Endianness
Bit packing within a single byte is endianness-independent. However, if you pack across multiple bytes, byte order matters and must be handled consistently.
Applications
- Embedded Systems: Configuration registers on microcontrollers often pack multiple flags and small values into single bytes.
- Network Protocols: Protocol headers (TCP flags, DNS record types) use bit-level packing to minimize packet size.
- Game Development: Storing multiple character attributes (health tier, weapon type, status flags) in limited memory.
- File Formats: Image formats like BMP store pixel data with specific bit depths per channel.
Conclusion
Compressing multiple numbers into one byte through bit packing is a straightforward technique that uses shift and mask operations. The key constraint is that the total bits across all fields must not exceed 8. Within that limit, the technique is lossless and efficient. The main costs are code complexity and the need for consistent field layout definitions across pack and unpack sites.

