01What do numbers look like inside a computer?

The numbers we see might be 13, -5, or 3.14. But there is no tiny card shaped like “13” sitting in computer memory.

A computer represents values as a string of bits—a combination of 0s and 1s—according to data types and encoding rules.

A number as we see it

For example, 13

Represented by a rule

For example, as an 8-bit unsigned integer

00001101

Bits in memory

Under this rule, these bits represent 13

02How are positive integers stored?

The easiest way to understand non-negative integers is to write the number in binary.

For example, decimal 13:

Decimal13
=
8+4+1
=
Binary1101₂

If we require it to take 8 bits, we add zeros at the front:

8-bit unsigned integer00001101

03Why do numbers use a fixed number of bits?

When a computer processes data, it needs to know how long each piece is. Integer types therefore usually specify a fixed bit width.

8 bits2⁸ = 256

Unsigned values range from 0 to 255

16 bits2¹⁶

There are 65,536 possible bit patterns

32 bits2³²

About 4.29 billion different patterns

64 bits2⁶⁴

An enormous number of combinations

For an unsigned integer, n bits can represent values from 0 to 2ⁿ - 1.

8-bit unsigned: 0 to 2⁸ - 1 = 255

04How are negative numbers stored?

Representing only zero and positive numbers is not enough. Programs also need to store values such as -1, -5, and -100.

Modern computers usually use two’s complement for signed integers.

+5 as an 8-bit integer00000101
Flip each bit11111010
Then add 111111011
-5 in 8-bit two’s complement11111011

With 8-bit two’s complement, values from -128 to 127 can be represented.

-128
0
127

05How are decimals stored?

Decimals are more complicated. Modern computers often use floating-point numbers to represent approximate real-number values across a wide range.

Common IEEE 754 floating-point formats divide a bit pattern into parts that represent the sign, significand, and exponent.

SignPositive or negative
ExponentHow large the scale is
SignificandImportant precision information for the value

Many decimal fractions cannot be represented exactly with a finite number of binary digits. For example, 0.1 in common binary floating-point formats is usually only an approximation very close to 0.1.

06How can the same bits have different meanings?

A string of bits is just a pattern. What it represents depends on its data type and interpretation rules.

01000001

As an unsigned integer

65

Interpreted as ASCII

A

As raw bits

01000001

07Can a number be too big to fit?

Yes. A fixed number of bits can provide only a limited number of combinations.

8-bit unsigned integerMaximum value: 25511111111₂

What if we want to add 1?

The result depends on the rules of the language, type, and runtime environment. Do not simply assume it will “automatically become a bigger box.”

Floating-point numbers have another limitation: their precision is limited as well as their range. Some values can only be stored approximately even when they fall within the range.

Range

How large or small a value can be

Precision

How finely and accurately a value can be represented

What you have learned

  • Numbers inside a computer are ultimately represented as bits according to a set of rules.
  • Non-negative integers can be represented directly using binary place values.
  • Integers usually have a fixed bit width, such as 8, 16, 32, or 64 bits.
  • An n-bit unsigned integer can usually represent values from 0 to 2ⁿ - 1.
  • Modern computers usually use two’s complement for signed integers, not a simple “top bit means minus” rule.
  • Decimals are often represented using floating point. Common formats follow IEEE 754, and many decimal fractions can only be stored approximately.
  • The same bits can be interpreted in different ways, so data types and encoding rules matter.
  • Fixed width limits a number’s range; floating-point numbers also have limited precision.

Next question: Now that numbers can live in a computer, how are the letters A, B, and C turned into numbers and stored? Next article: How Do Letters Become Numbers?