DEV Community

Cover image for Binary numbers and their arithmetic operations
Keema
Keema

Posted on

Binary numbers and their arithmetic operations

"There are 10 kinds of people in the world,
those who understand binary numbers, and those who don't."

From the beginning of human history in order to count people have been using the decimal number system(base 10): the numbers that consist of digits from 0 to 9. There’s no exact explanation for this, but it’s accepted to think it’s because humans have 10 fingers. It’s always been that way and the human’s brain is starting to get used to it when they’re born. But now, beyond human brains there are computers that are being used to count numbers, too, and even more than humans do. But they’re not getting used to it from childhood, right? So the human had to “teach” it to computers.

But as long as we had the opportunity to do that, we could use it more rationally. If the computer used 10 digits to understand the information, it would be harder for it, compared to how they actually do now. The computers use the binary number system(base 2), which consists of 0s and 1s only, which are called binary digits. It helps the computer to work faster and use less energy, memory, and less resources overall. Just to make it clear, the computer sees the 0s and 1s as ons and offs respectively, hence it has to analyze two states only instead of 10.

Binary numbers look just like these: 1100101, 1111111, 101, 10000000, 10101010, etc. Each of these 0s and 1s are called bits. 8 bits make up 1 byte, which are basically the bytes we all know, which make up kilobytes, megabytes, etc., that are used to represent the memory of the device. Therefore, using decimal numbers for computers would obviously require more memory. That’s why the binary system is much more rational to use in the case of computers.

So logically, every decimal number can be represented as a binary number, and the same for the opposite. There’s a certain logic that we use to convert the decimal numbers to binaries. Starting from the left, we write numbers from 0 under the digits. These are the powers of 2s. Then we multiply these 2ns with 0 or 1, according to which of them is written above it. After that we add all the numbers and get the final answer, which is the decimal representation of our binary number.

For example, we have the binary number 100111. Now we write the integers from 0 under each of the binary digits.

                    1 0 0 1 1 1
                    5 4 3 2 1 1
Enter fullscreen mode Exit fullscreen mode

And now we multiply the binary digits with the 2n, where n is the number written under the binary digit. So we do:

     1*25+0*24+0*23+1*22+1*21+1*20=25+22+21+20=39
Enter fullscreen mode Exit fullscreen mode

Hence, the binary number 100111 is the number 39 in a decimal number system.

To convert the decimal number to binary number, we need to understand what powers of 2 is the number between. For example we have the number 221. It is greater than 27 and smaller than 28. We take the smaller one and write all the powers of 2s smaller or equal to it. We’re doing it up to 27.

                    128 64 32 16 8 4 2 1
Enter fullscreen mode Exit fullscreen mode

Now starting from left we take all the numbers which we need in order to get our decimal number. If we need the number, we write a 1 under it, if we don’t, we write 0.

                1281 641 320 161 81 41 20 11
Enter fullscreen mode Exit fullscreen mode

Writing all the 0s and 1s respectively from left to right, we have the number 11011101, which is the binary representation of decimal number 221.

Just like we can add the decimal numbers, we can do the same for binaries, too. It’s basically based on 3 equations:

  1. 0+0=0
  2. 1+0=0+1=1
  3. 1+1=10

Let’s just have an example:
1 1 0
(+) 1 1 1

Let’s consider all the columns from left to right. So 1+0 is 1, we write it under the first column. Then 1+1 is ten. Now we write the 0 under the 2nd column and take the 1 as a carrier and write at the top of the 3rd column. Now we have 1+1+1. As we already know that 1+1 is 10, then adding another 1 would become 11. So we write 11 under the 3rd column.

                  1      
                  1 1 0 
             (+)  1 1 1
                  ↓—↓—↓
                1 1 0 1
Enter fullscreen mode Exit fullscreen mode

Thereby, adding the binary numbers 110 and 111 we get 1101. We can check ourselves by just finding the decimal values of these numbers and checking the addition for them.

Binary numbers can be multiplied as well. We use these three main equations:

  1. 0 × 0 = 0
  2. 0 × 1 = 1 × 0 = 0
  3. 1 × 1 = 1

Let’s have an example of multiplying two binary numbers:

                           1 1 0 
                     (×)   1 1 1
Enter fullscreen mode Exit fullscreen mode

We do multiplication just like how we do it for decimals: every digit of the 2 starting from left multiplying with the digits of the 1st number. Then adding the results just like we did in the example of adding binary numbers.

                          1 1 0 
                     (×)  1 0 1
                       ————————————
                          1 1 0 
                    (+) 0 0 0
                  (+) 1 1 0 
                   ———↓—↓—↓—↓—↓———
                      1 1 1 1 0
Enter fullscreen mode Exit fullscreen mode

Accordingly, the product of binary numbers 110 and 101 equals 11110. We can check this one by just finding the decimal values of these numbers and checking the product for them.

Binary numbers are commonly used in computer applications. All the coding and languages in computers such as C, C++, Java, etc. use binary digits 0 and 1 to write a program or encode any digital data. The computer understands only the coded language. Therefore these 2-digit number system is used to represent a set of data or information in discrete bits of information.

Top comments (0)