"how numbers are stored and used in computers"
Humans represent numbers in base 10 - that is, with 10 different digits.
Try clicking the digits below to update the formula.
Each digit corresponds to a power of 10. Humans also occassionally write numbers in base 16, where the letters a
through f
represent the numbers 10
through 15
. This is commonly seen in web programming as a way to specify a color.
Click the digits or change the color to update the hex code.
Regardless of which human representation you choose, a computer will represent that number in base 2, with only the digits 0 and 1. This roughly corresponds to the presence or non-presence of an electrical current.
Each digit of a binary number is referred to as a bit. You will often see binary numbers described in terms of their bit length, such as the 8 bit integer below.
We call it an integer because it can only store whole numbers. Since there is no way to specify a negative number, we would also call this an unsigned integer.
A group of 8 bits is called a byte, where each byte can represent 28 (256) different values. By combining multiple bytes, a number can have a greater range of possible values.
In the example above, we are using 8 bits to represent an unsigned integer. If we want to store negative numbers, we can dedicate 1 bit as a sign bit to store 0 for positive numbers and 1 for negative numbers.
If the sign bit simply controlled the presence or absence of a negative sign (-
), there would be two binary numbers that both represent zero -
To avoid ambiguity, and to allow the same circuits to be used for both signed and unsigned numbers, computers represent negative numbers in two's complement. If the sign bit is 1
, then all bits are inverted, so 1
is considered a 0
, and 0
is considered a 1
- the sum is then incremented by 1, and the result is multiplied by -1 to get the negative value.
Just as we express long distances with kilometers instead of meters, we can express large amounts of binary information with prefixes that indicate their order of magnitude. Unlike traditional scientific prefixes which are expressed in powers of 10 (e.g. 1 kilometer is 1000 meters), binary prefixes often refer to the closest power of 2, so a kilobyte is technically 1024 bytes (
Here is what one kilobyte (1024 bytes) of information looks like. This isn't just any kilobyte though - it is De rerum natura, the ancient wisdom of Lucretius.
Each character of Lucretius' poem is encoded with a single byte, using a format called ASCII. This format works well for English letters, but is woefully inadequate for modern text which contains a wide range of characters like emojis and Chinese letters. Most modern systems use encoding formats like UTF-8 and UTF-16, which can use multiple bytes to encode each character.