DEV Community

Armen
Armen

Posted on

Introduction to the world of numbers in computers

Introduction

Surely, many of you wondered how a small computer or any other technological device works. It is really fascinating when you start to understand the principles behind computers. Basically, computers do what people tell them to do. But, how? Do they understand human language? This is a question I believe everyone asked themselves at some point of life. And the answer is very simple: NO, computers do not understand human language. So, there is another logic that computers use in order to communicate with humans or with each other. Modern computers use electricity to work and inside a microchip the electricity is either switched on or off. The existence of electricity is represented by 1 and 0 which is called the binary number system. So, computers understand us because whatever we write in it, it converts the input into binary numbers and understands it.

The idea of different number representations

There are many numerical systems such as the one that we use, the decimal system or base 10. Since we have only 10 fingers, there are 10 digits that we use to represent any number. But this does not mean that there cannot be a numerical system that has more than 10 symbols. In fact, there are base 11, base 12, base 16 and so on. In order to represent a number in base 16, after the digit 9, as we have no more digits, we start to use letters (A, B, C, etc.). For example, the number 2 in decimal is just 2, but in binary we do not have the symbol “2” so we write 10 which is not “ten”, it is “one and zero”.

The corresponding binary number for its decimal

Conversions between different number representations

So, how do we know the corresponding binary number for its decimal or vice versa. Look at the picture below.

Conversions from decimal to binary
In this picture we want to convert the decimal number 25 to binary number. For that, we will use the simple mathematical division method. Since we want to convert it into base 2, we need to divide 25 by 2. 25 is not divisible by 2, so we write 12 and the remainder 1. Then, we should divide 12 by 2. Here, 12 is divisible by 2 so we have 0 remainder which we must write too. So, by using this principle we divide decimal numbers by 2 and write remainders. In the end we should write the remainders in reverse order and BOOM!! We have the corresponding binary number for 25 which is 11001.

  • In order to convert the binary number into decimal we need to write the binary number and start to count it starting from the end and from 0 (Check out the picture below).

Conversions from binary to decimal

Then, the counted numbers we need to use as the power of 2 (because we are in base 2). For the first cell we write 2^0 and multiply this by 0 because we have 0 inside the cell. For the second cell we write 2^1 and multiply this by 1 because here we have 1 inside the cell. We keep doing this till the last cell and start to count the results for each cell. After that we add them together and get the decimal number (here 42).

  • We can use the exact same method to convert any number into any numerical system.

Addition, subtraction, multiplication and division of binary numbers

Like in the decimal system, in binary too, we can do basic arithmetic operations with numbers such as addition, multiplication, etc. In order to do that we should again use the same rules and methods as usual.

  • The picture below shows the operation of adding two binary numbers.

Addition of binary numbers

In order to simplify our work a little, we can think like this: if we want to add the binary 1 by 1, we can do their addition in decimal (in mind), the result is 2, right? What is the binary number for 2? Yeah, you are right, it is 10. So, 1+1 in binary is equal to 10. This method is very helpful especially when we need to work with big numbers when we need to add four or five “1”. In that case, in decimal 1+1+1+1 = 4, in binary 4 = 100, so, 1+1+1+1 = 100.

  • Multiplication of binary numbers is very simple because there is nothing new to tell.

Multiplication of binary numbers

At first we need to do the multiplication of zeros and ones, the exact way that we would do in decimal, then, we need to do the additions and we are done.

  • Subtraction is a bit harder to understand compared to multiplication. Again, the same rules of usual subtraction apply here. When we add two binary numbers we put the carry at the left column and then continue to add using that carry. In case of subtraction, when we cannot subtract 1 from 0 we borrow 1 from the left column (if there is 1, if not we keep going left till we find 1).

Subtraction of binary numbers

For example, in this picture, in the first column we need to subtract 1 from 0, so we borrow one from the left column, so the first column becomes two ones (1, 1). Subtracting 1 from 2 we get 1 and go on.

  • Division of binary numbers is the most difficult arithmetic operation compared to others. Usual rules and methods do apply here.

Division of binary numbers

In this example, the number 10010 is the dividend and the number 11 is divisor. At first we need to find a part in number 10010 that starts from that part the number is greater than 11. In this case, 1 is not greater than 11, 10 also, but 100 is already greater than 11 so we start by dividing 100 by 11. How many 11 we have in 100? Again, it is a good idea to do decimal divisions in mind. Since we have one 11 in 100, we write 1 above and start to do subtraction. By doing the simple operations here we get the answer 11.

  • Important advice: It is always a good idea to double check the answers in decimal form.

Negative binary numbers

Since in base 2 we have only two symbols 1 and 0, we cannot represent -1 by adding a minus sign in front of the number. So, we need to represent negative binary numbers by putting an extra byte which contains either 0 for positive number or 1 for negative number.

Negative binary numbers

Top comments (0)