DEV Community

Cover image for Binary Number System
Sona Barseghyan
Sona Barseghyan

Posted on

Binary Number System

Hello friends! I’m glad that you’re reading my blog and I hope that after finishing it you will get the answers to your questions. The topic of my blog is binary numbers and some basic information connected with them. So, if you are interested in programming and want to explore this fascinating field deeper, then let’s get started!
First of all, what are binary numbers? What do we understand from saying “binary”? We all are used to the ordinary decimal numbers from 0 to 9 which we use to count and do several operations.
But binary numbers are numbers expressed in the base-2 numerical system. This is a method of mathematical expression which uses only 2 symbols: “0” and “1”. The word “binary” itself indicates about its meaning-something consisting of two parts.
So, if we can count with decimal numbers in a regular way, then it’s also possible with binary numbers. But the representation of numbers in binaries is a bit different. We are allowed to use only “0”s and “1”s. For example, if in decimal numbers the first 10 numbers are 0,1,2,3,4,5,6,7,8,9, then in binaries they are 0,1,10,11,100,101,110,111,1000,1001. As you can see, these are the first 10 possible numbers from smallest to the greatest that can be made with “0” and “1”. But you can ask what is the purpose of using only these 2 numbers. It is easy to store them in the computer as “1”-on and “0”-off.
Now that we’ve covered the description of binary numbers, we can pass to the part of converting binaries into decimals and vice versa. Let’s assume that we have binary number 10101. As mentioned above, we are dealing with base-2 system, so we will need the powers of 2(2*0, 2*1, 2*2, 2*3, 2*4 and so on). So, starting from the right side we assume the numbers to have their order: the first one in the right is 0th, the second one is under 1, the third one is under 2 and so on. So to get the decimal representation of the binary, we take all the elements of the binary from the right, multiply them by 2 to the power of their digital position(starting from 0) and add all the results. For example, the number 10101=2*0x1+2*1x0+2*2x1+2*3x0+2*4x1=1+4+16=21.
Here is an image illustrating it for a better understanding:

Image description

We can have a case when the binary number is fractional. For example 110.01. In this case, we consider the 0 positioned element the one before the point, and after the point to the right, the digital position starts with -1 and goes to -2, -3, etc. So, 110.01=2*0x0+2*1x1+2*2x1+2*-1x0+2*-2x1=2+4+0.25=6.25.
Here is the visual explanation of it.

Image description

Now it’s time for converting decimals into binaries. We will need the powers of 2. For representing a decimal as a binary we must write the powers of 2 in an increasing order from the right side. Suppose we have the number 83 and we want to write it as a binary. We need the powers of 2(from right to left) from 2*0 to the one which is the greatest number smaller than 83. That number is 2*6=64. First, we subtract the 64 from 83 and get 19. Now we are going to use the greatest power of 2 smaller than 19. That is 2*4=16. Then, similarly, we do 19-16=3 and use the greatest power of 2 smaller than 3, which is 2*1=2. Then by doing 3-2, we get 1 which we simply add to the powers of 2 that we used. We used 2*0, 2*1, 2*4, 2*6. So, we write “1”s in the positions of the used powers and “0”s in the positions we didn’t use. That gives us 1010011. See the image.

Image description

There is also another way of converting decimals into binaries. Let’s consider the same 83. We start by dividing it by 2. 83/2=41 with a remainder 1. Then we take 41, divide by 2: 41/2=20 with a remainder 1. Similarly, 20/2=10 with remainder 0. 10/2=5 with a remainder 0. 5/2=2 with a remainder 1 and 2/2=1 with a remainder 0. And finally we do 1/2 and obviously have a remainder, we denote the remainder by 1. Our binary number is going to be all the remainders side by side, but in reverse sequence. We get 1010011.
See the image.

Image description

Now some information about binary operations. Let’s start with addition. We have to remember that 0+1=1(like decimals), but 1+1=10. The remaining part is all like the addition of decimal numbers. Let’s consider the binaries 100110 and 11011.
See the image for better understanding.

Image description

Next we’ll talk about the subtraction of binary numbers. Again, it’s similar to the subtraction of decimal numbers. The only difference is that if in the case of decimals we borrow 1 from the previous element when we have 0, and the element gets the value 1, in binary numbers, after borrowing from the previous term, the element gets the value 2. An example is illustrated:

Image description

It’s time for the multiplication of binaries. It is again similar to the multiplication of decimals, the most fundamental things are that 1x1=1, 1x0=0. We multiply the last element of the second number with all the elements of the first number, add the multiplication of the second element from the right with all the elements of the first number and so on. And the addition of the result can be easily done in the way we discussed before. See the example:

Image description

And the last operation is the division of binaries. Again, the principle of the division is the same as in decimals, we take the first term of the dividend, see if it is bigger or smaller than the divisor and, accordingly, proceed. In the case of being small, we write 0 in the result and go to the second term, and so on. Let’s see it on an example:

Image description

So, this was the basic information connected with binary numbers, their representation and the operations made with them. I hope this was helpful, and I genuinely wish you all good luck in your exciting journeys!

Some references:
https://youtu.be/VKemv9u40gc
https://youtu.be/C5EkxfNEMjE
https://youtu.be/VLflTjd3lWA
https://youtu.be/7CbMrByB5Lk

Top comments (0)