Computers implement numbers as tiny groups of transistors that can store individual binary digits called “bits”. Every bit can be either 0 or 1 – only one of these two numbers. The circuity that forms bits can be either “on” – then the value stored there is 1, or “off” – then it is zero.
Groups of consecutive bits can store larger numbers, for example, 8 put together bits can store numbers from 0 to 255.
Because a computer “digit” can only be 1 or 0, we say that computers calculate in “binary” system.
Consider a decimal number, for example 812. 8 is the number of “hundreds” in that number, 1 is the number of “tens”, and 2 is the number of “ones”. In a longer number, let’s say 7854320 has 7 “millions”, 8 “hundreds thousands”, 5 “tens thousands”, 4 “thousands”, 3 “hundreds”, 2 “tens”, and 0 “ones”.
812 = 8 * 100 + 1 * 10 + 2 * 1
7854320 = 7 * 1000000 + 8 * 100000 + 5 * 10000 + 4 * 1000 + 3 * 100 + 2 * 10 + 0 * 1
Numbers like “tens”, “hundreds”, “thousands”, “millions” tell us how to treat a number in a specific position in the number. Third number is thousands, second number is hundreds… and so on. They are all powers of 10: 1 is 100, 10 is 101, 100 is 102, 1000 is 103, etc.
Generally, to form a number a digit in position N is multiplied by 10N.
7854320 = 7 * 106 + 8 * 105 + 5 * 104 + 4 * 103 + 3 * 102 + 2 * 101 + 0 * 100
Computers reason about numbers very similarly, but they only have two digits, 0 or 1, so their “ten” is actually 2.
| Binary | Decimal |
| 0 | 0 |
| 1 | 1 |
| 10 | 2 |
| 100 | 4 (2 * 2) |
| 1000 | 8 (2 * 2 * 2) |
| 10000 | 16 (2 * 2 * 2 * 2) |
| 100000 | 32 (2 * 2 * 2 * 2 * 2) |
| 1000000 | 64 (2 * 2 * 2 * 2 * 2 * 2) |
| 10000000 | 128 (2 * 2 * 2 * 2 * 2 * 2 * 2) |
So therefore 11 in binary (two bits of a byte on) is 1 * 2 + 1 * 1 = 3.
101 in binary is 1 * 4 + 0 * 2 + 1 * 1 = 5.
1111 in binary is 1 * 8 + 1 * 4 + 1 * 2 + 1 * 1 = 15.
Binary arithmetic works extremely similar to digital arithmetic, for example, when you add numbers:
5 + 3 = 8 in decimal would be this in binary:
0 0 0 0 1 0 1
+
0 0 0 0 0 1 1
—————-
0 0 0 1 0 0 0
The rules of addition in binary:
0 + 0 = 0
1 + 0 = 0 + 1 = 1
1 + 1 = 10
To store numbers larger than 255 groups of bytes are used. For example, 2 bytes can store numbers from 0 to 65535 and 4 bytes can store numbers from 0 to 4,294,967,295.
Exercises
Express decimal 31, 65, and 137 in binary.
What is binary 100111011 in decimal?