Learning path

Full curriculum

Full curriculum

Unit content

Bits, bytes and integer encodings

A bit is a binary digit with value $0$ or $1$. Digital data is built from finite sequences of bits.

A byte conventionally contains eight bits, so one byte has

$$2^8=256$$

different possible bit patterns.

Unsigned integers

With $n$ bits, an unsigned binary integer can represent values from

$$0$$

to

$$2^n-1.$$

For example, eight unsigned bits represent $0$ through $255$.

Signed integers

Computers commonly encode signed integers using two's complement. In an $n$-bit two's-complement representation, the range is

$$-2^{n-1}\le x\le2^{n-1}-1.$$

The most significant bit has weight $-2^{n-1}$ while the remaining bits keep their usual positive powers of two. Equivalently, negating a fixed-width value can be performed by complementing its bits and adding one.

This representation lets the same fixed-width binary addition circuitry work naturally across positive and negative values.

Fixed width and overflow

A fixed-width representation has only finitely many patterns. If an arithmetic result lies outside the representable range, overflow occurs.

The mathematical integer and its machine representation are therefore not identical concepts: mathematical integers are unbounded, while a machine integer has a chosen width and encoding.

Bytes as raw storage

A byte by itself does not say what it means. The pattern 01000001 could be an integer, part of a text encoding, a pixel component or an instruction depending on how the surrounding system interprets it.