Integer Width and Range Tradeoffs
What You'll Learn
You'll learn how the number of bits allocated to an integer directly determines what values it can represent—and why choosing the right width matters.
The Core Idea
Every integer in a computer is stored using a fixed number of bits. The width (8, 16, 32, or 64 bits) determines how many unique values you can represent. More bits = wider range, but also more memory.
Think of it like a odometer: a 3-digit odometer can count 000–999 (1,000 values), while a 6-digit one goes 000,000–999,999 (1,000,000 values). More digits = bigger range, but takes up more space on the dashboard.
Common Integer Widths
For unsigned integers (non-negative only):
- 8 bits: 0 to 255 (2⁸ = 256 values)
- 16 bits: 0 to 65,535 (2¹⁶ = 65,536 values)
- 32 bits: 0 to 4,294,967,295 (~4.3 billion)
- 64 bits: 0 to 18,446,744,073,709,551,615 (~18 quintillion)
For signed integers using two's complement:
- 8 bits: -128 to 127
- 16 bits: -32,768 to 32,767
- 32 bits: -2,147,483,648 to 2,147,483,647
- 64 bits: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
The Tradeoff
Wider integers = more range but consume more:
- Memory (storage)
- Cache space (performance impact)
- Bandwidth (data transfer)
Narrower integers = save resources but risk overflow if values exceed the range.
Use int8 for small counters, int32 for general purposes, int64 for large calculations like timestamps or file sizes.
Key Takeaway: The bit-width you choose creates a direct tradeoff between representable range and resource consumption—pick the smallest width that safely holds your expected values.