DEV Community

Zhanna Balyan
Zhanna Balyan

Posted on

All about binary numbers

Hey everyone. In this post we are going to talk about binary numbers and dig a little further on the operations we can do with them. In our everyday life we always use the decimal numbers from 0 to 9 in order to represent, count and do operations with different numbers. Any possible number we can think of consists of those digits. However, the computers don’t work this way. They only use two digits, which are “0” and “1”. All the numbers can be expressed using these two digits. The system, consisting of only two digits “0” and “1”, is called a binary system or a base 2 number system. The digits are also called binary digits or simply bits. Bits are the smallest units of data and by combining bits we create larger units like bytes, which computers use to store and process information. Binary number system is the base for all binary codes used in computing systems. There are numerous operations that we can perform on binary numbers. For example, by knowing the decimal number we can convert it into a binary number and vice versa. In order to perform these two operations, we need to be familiar with the powers of two because that is what we are going to use during the conversion process. This can be done in two ways and I’m going to show both of them by also using examples. The first method is the subtraction method. Let’s take the number “55” and show how we can convert it into a binary by using the subtraction method. First, we need to list the powers of two until we reach a number that exceeds our number.

64, 32, 16, 8, 4, 2, 1

Then we find the greatest power of two which is less than “55” and subtract it from the number.

55-32=23

Then we take the highest power of two which is smaller than the result and subtract it and repeat the process until the result is “0”.

55-32=23-16=7-4=3-2=1-1=0

We then represent the number by the sum of the powers of two that we used during the process.

55→ 32+16+4+2+1

After that, we start putting a “0” in the places of the powers of two that we haven’t used during the subtraction and “1” in the places of the powers of two that we’ve used.

64(0), 32(1), 16(1), 8(0), 4(1), 2(1), 1(1) 110111

In this example we got “110111” which is a binary number. We can also convert decimals into binary numbers by using the method of division. Here we just need to divide the number by two to the point where we reach “1” and write the remainders. Then we simply place the remainders from bottom to up and get our binary number, in this case “110111”. Since it’s a little inconvenient to type an example here, I am going to use a picture for you to understand it better.

Image description

When we master this process, we can easily do the opposite operation, convert the binary into a decimal. For this, we again need to use the powers of two. Let’s carry on with our previous example. If we want to represent “110111” as a decimal number we need to complete these basic steps. 1) Find corresponding decimals for each bit in the increasing order from right to left

110111 (543210)

2)Represent each of the bits as a product of 2 to the power of its corresponding decimal value and the bit itself. For example 1= 2^5x1

3)Add all the products together to get the decimal number. 2^5x1+2^4x1+2^3x0+2^2x1+2^1x1+2^0x1= 32+16+4+2+1=55

And that’s how we get our result.

After we recognize and get comfortable with the concept of binary numbers and how they are represented, we can easily do mathematical operations with them such as addition, multiplication, subtraction and division.

The addition of two binary numbers is done by a method well-known to us from school. But we need to be careful because in the binary system “1+1” means “10”. So, we add binary numbers the same way we add decimals and when we have “1+1” we write “0” and carry the “1” to the next column. It’s a pretty easy process and you can find an example below where the process is shown step by step.

The same process applies to the multiplication. Here, again, we need to be careful in the final part of the operation where we add the results we got because when we add “1+1”, we write “0” and carry the “1” to the next column (see the example for better understanding).

Image description

The subtraction of binary numbers is done pretty much the same way. Except, here we need to remember that “0-1” equals to “1”. Because 0 is less than 1, we can’t subtract it, so we borrow the “1” from the next column and continue our process following from that.

And the last operation we’re going to talk about is the division of two binary numbers. Once again, the process is quite similar to the division we all know from school. We divide the binary numbers just like we divide the decimals. We only need to take into account the fact that if there is a subtraction “0-1” we need to borrow 1 from the next digit and write “1”. Here, again, I’ve illustrated the process with an example.

Image description

By far, we’ve covered everything you need to know about binary numbers and the simple mathematical operations that can be performed on them. Hope this post helped you find the answers to your questions! Thank you all for reading and your attention.

Top comments (0)