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
00001101Bits 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:
If we require it to take 8 bits, we add zeros at the front:
0000110103Why 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.
Unsigned values range from 0 to 255
There are 65,536 possible bit patterns
About 4.29 billion different patterns
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 = 25504How 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.
00000101111110101111101111111011With 8-bit two’s complement, values from -128 to 127 can be represented.
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.
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.
01000001As an unsigned integer
65Interpreted as ASCII
AAs raw bits
0100000107Can a number be too big to fit?
Yes. A fixed number of bits can provide only a limited number of combinations.
11111111₂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?