Data representation · foundation

Bits, bytes, and integer width

A bit has two possible values, while a byte is the smallest independently addressable storage unit in a machine model and is eight bits on nearly all modern general-purpose systems. An integer's width is the number of bits in its representation and determines its finite set of bit patterns.

Why it matters

Widths control numeric range, storage size, binary formats, overflow behavior, and interoperability. Code that assumes every integer has the same width can fail at file, network, language, and architecture boundaries.

Mental model

How to reason about bits, bytes, and integer width

An n-bit field has 2^n distinct patterns. A type's signedness and language rules map those patterns to values and define what happens when a computation exceeds the representable range.

Analogy

Integer width is the number of switches on a panel: more switches create more possible configurations, while the labelling scheme decides what each configuration means.

Examples

See the boundary, not just the happy path

Worked example · Count an unsigned byte's values

An 8-bit unsigned integer represents 0 through 255

Eight bits provide 2^8 = 256 patterns. Assigning the lowest pattern to zero makes the highest unsigned value 256 - 1.

Worked example · Choose an explicit protocol width

Store a packet length as an unsigned 16-bit field

The format allocates exactly two eight-bit octets and can encode 0 through 65,535. Sender and receiver must also agree on byte order, not only width.

Useful contrast · Value does not reveal width

The value 42 fits in uint8, uint16, uint32, and arbitrary-precision integers

A mathematical value and its representation are different facts. Context supplies width, signedness, encoding, and overflow behavior.

Common mistakes

Misconceptions to remove early

Assuming every language integer has a fixed machine width

Some types are fixed-width, some depend on the platform, and others grow to arbitrary precision. Consult the language and serialization format rather than inferring from the word integer.

Ignoring overflow semantics

Exceeding a type's range can wrap, trap, raise, saturate, or be undefined depending on the language, type, build mode, and operation. Width alone does not specify the outcome.

Quick check

Can you predict the result?

1. How many distinct patterns does a 12-bit field have?
  • 4096
  • 2048
  • 12
Answer: 4096
2. Why is saying a value is 100 not enough to determine its storage representation?
Answer: The width, signedness, encoding, byte order, and language or format rules are still unspecified.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster