Binary numeral system
Published on 2018-04-02
Each computer system is composed of a number of digital circuits (i.e. circuits with two possible states: 1 - on, and 0 - off). In computer systems, all data (numerical values: 58, -123, 3.14; characters: t, A, *; sentences: "Hello everyone!"; images and more) can be stored using combinations of zeros and ones (so-called binary digits or bits).
In particular, data can be represented in several ways: by difference in voltage (this happens with transistors), magnetic polarities (happens with hard disks), microscopic points that reflect light differently (occurs in compact discs), etc. For example, a transistor with a voltage of 5 volts can be considered on (1), while a transistor with a voltage of 0 volts can be considered off (0).
Before we discuss the terms binary numeral system and binary number, it is important to recall a few basic mathematical notions. At school, when we first met the decimal number system (the system we use to represent numbers), we were told that the basic values (digits) are organized in columns (ones, tens, hundreds, etc.), and that each column is 10 times more significant than the previous one. Take, for example, the number 3678 (3 thousand, 6 hundred, 7 tens and 8 ones):
3 | 6 | 7 | 8 = 3*1000 + 6*100 + 7*10 + 8
Obviously, there is an important connection between the decimal number system and the powers of the number 10. To get the value of an integer, we need to multiply the digit of ones with 100=1, the digit of tens with 101=10, the digit of hundredths with 102=100, the digit of thousands with 103=1000, etc., and sum all the results. Specifically,
3678 = 3*103 + 6*102 + 7*101 + 8*100 = 3000 + 600 + 70 + 8 = 3678
The decimal number system uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 to represent numeric values (numbers). Let's try to count in the decimal number system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, stop. Now we have a problem - there are no more digits to write. We continue counting by adding a new column of digits (tens), which will be ten times more valuable than the previous column (ones). Now, we continue: 10, 11, 12, 13, 14, ..., 98, 99, stop. Now we add a third column of digits (hundredths) and continue: 100, 101, 102, ... Please, note that the most significant column changes least often, while the least significant column (ones) changes continuously.
Now, let's see how we can represent all these numbers in the (so-called) binary numeral system. The binary system is a numerical system that enables us to represent numerical values using two symbols (two digits): 0 and 1. The binary numeral system functions in the same way as the decimal numeral system, with the difference that instead of columns that are ten times more valuable than the corresponding columns in front of them (103, 102, 101, 100), they are two times more valuable (23, 22, 21, 20). Of course, instead of 10, we only need 2 digits (0 and 1).
As we did with the decimal number system, let's try to count in the binary number system: 0, 1, stop. We do not have any more digits to write. We add a new column of digits, which is worth two times the previous column. We continue to count: 10, 11, stop. We add another column. We continue counting: 100, 101, 110, 111, stop. We add a new column, and continue: 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, ...
The following table shows the numbers from 1 to 20, represented in the decimal and in the binary number system.
decimal number system | binary number system |
---|---|
1 | 1 |
2 | 10 |
3 | 11 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
15 | 1111 |
16 | 10000 |
17 | 10001 |
18 | 10010 |
19 | 10011 |
20 | 10100 |
Conversion (from binary to decimal)
How can we convert a certain number represented in the binary number system into the decimal number system? Simple, we just need to sum the products of the binary digits with the values of the columns in which they are located (powers of the number 2). For example, the binary number 1001001101 can be converted to decimal form by computing the following sum:
[1 * (29)] + [0 * (28)] + [0 * (27)] + [1 * (26)] + [0 * (25)] + [0 * (24)] + [1 * (23)] + [1 * (22)] + [0 * (21)] + [1 * (20)] =
= [1 * 512] + [0 * 256] + [0 * 128] + [1 * 64] + [0 * 32] + [0 * 16] + [1 * 8] + [1 * 4] + [0 * 2] + [1 * 1] = 512 + 64 + 8 + 4 + 1 = 589.
So, 1001001101 (represented in binary) is equal to 589 (represented in the decimal number system). Computers don't store the value 589 directly - on a hard disk or in memory, as we do on a sheet of paper. Instead, they convert the number 589 into the binary numerical system and represent what we know as 589 with 1001001101, through a combination of transistors with a value of 1 (voltage) and 0 (no voltage). As we mentioned previously, it does not have to be a transistor with voltage - instead, in different parts of the system (hard drives, compact discs, memory, etc.), the recording is done differently.
There are 10 types of people in the world: those who understand binary numbers and those who don't.
Try to convert the following binary numbers (left column) into the decimal number system.
binary number system | decimal number system |
---|---|
100 | 4 |
1011 | 11 |
111001 | 57 |
10000110 | 134 |
Conversion (from decimal to binary)
It remains to be shown how one number can be converted from decimal to the binary number system. Let's start with the simpler way of doing it: by subtracting the largest power of 2.
Let's try to convert the number 589. We begin by generating all powers of 2 smaller or equal to 589: those are 1, 2 (1*2), 4 (2*2), 8 (4*2), 16 (8*2), 32 (16*2), 64, 128, 256 and 512 (the next number is 1024 and 1024 is larger than 589). Afterwards, we subtract the largest power of two from 589 (that is 512). 589-512 = 77. We now proceed with the same procedure, only from the number 77. What is the largest power of 2 that can be subtracted from 77, after which we will get a number which is greater than or equal to 0? That's the number 64. 77-64 = 13. We continue with the same procedure, only from the number 13. What is the largest power of 2 that can be subtracted from 13? That's 8. 13-8 = 5. We continue with 5. 5-4 = 1. We continue with 1. What is the largest power of 2 that can be subtracted from 1, after which we will get a number greater or equal to 0? That's 1. 1-1 = 0. We get 0 and the procedure ends.
Now write the powers of 2 (1, 2, 4, 8, 16, 32, 64, 128, 256 and 512) from right to left (1 on the right) and place the value 1 in the columns corresponding to the powers that we subtracted from (512, 64, 8, 4 and 1) and the value 0 in the other columns.
Well, what is the binary value of 589?
512 | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1
- - - - - - - - - - -
1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1
We calculated the value 1001001101 by putting a one (1) in the columns corresponding to the powers of 2 we subtracted from (512, 64, 8, 4 and 1). All other binary digits are 0.
So, 589 (decimal) is equal to 1001001101 (in binary).
Try to convert the following numbers into the binary number system.
decimal number system | binary number system |
---|---|
7 | 111 |
15 | 1111 |
285 | 100011101 |
6123 | 1011111101011 |
We will finish this post by looking at another way to convert numbers from decimal to the binary number system: i.e. the one that is taught at school (at least it was taught there when I went to school). The procedure goes like this: first, we need to make a table with 2 columns - the first column will indicate the current number, while the second column will indicate whether that number is odd or even (if the number is even, we write 0; and if it's odd and thus not divisible by 2, we write 0). Then, we begin with the number we want to represent in binary (in our case 589), and we write 1 or 0 in the second column if the number is odd or even (respectively). In this case, 589 is an odd number and so we write 1. Now, we divide the current number with 2 and we proceed in the same way. In our case, we divide 589 with 2 (the result is 294, after throwing away the remainder) and we write the result in the next row of the table. We continue this procedure until we get the value 0 (we do not write that line in the table).
current number | parity |
---|---|
589 | 1 (because 589 is an odd number) |
294 | 0 (because 294 is an even number) |
147 | 1 (because 147 is an odd number) |
73 | 1 (because 73 is an odd number) |
36 | 0 (because 36 is an even number) |
18 | 0 (because 18 is an even number) |
9 | 1 (because 9 is an odd number) |
4 | 0 (because 4 is an even number) |
2 | 0 (because 2 is an even number) |
1 | 1 (because 1 is an odd number) |
The binary value can be read from the second column in the table, viewed from the bottom up (1, 0, 0, 1, 0, 0, 1, 1, 0, 1). So, 589 (decimal) is equal to 1001001101 (in binary).
The two procedures always result in the (same) correct value. Use the procedure that suits you best.