DEV Community

Lilit Babakhanyan
Lilit Babakhanyan

Posted on

Binary numbers/operations with binary numbers

What are binary numbers?

We use a decimal (base 10) number system in our everyday life. It has 10 values from 0-9, and any other number greater than 9 can be represented by those 10 digits. However, the decimal number system is too complicated for computers to understand. That is why we use a binary number system. In a binary number system, there are only two values, 0 and 1. Any other number can be represented with those two digits. Also, the binary system works well for computers because, in electric cycles, there are also two states when the electricity is on (1) and off (0).

How to convert decimal to binary?

There are 2 ways to convert decimal numbers to binary.

  1. The First method. Let’s convert 67 to binary If we write 67 as a sum of powers of 2, it’s going to be

Image description

Let’s write down a few numbers that are powers of 2.

Image description

The largest number that is a power of 2 equal to or less than 67 is 2^6 = 64. So, to get 67, we will need that 64, so we write 1 in the first place.

Image description

To get 67, we also need 2^1 and 2^0, so we write 1s in places of 2^1 and 2^0 and 0s in all other places.

Image description

We got 1000011 which is the binary representation of 67.

  1. The second method is the division method We need to take a decimal number and divide it by 2. If we get a reminder, we write 1. If we do not get a reminder, we write 0, and so on. Let’s again take 67.

Image description

And we do this until the end.

Image description

And we got 1000011 which is 67 represented in the binary number system.

How to convert binary to decimal?
To convert a binary number to decimal, let’s write powers of two under the binary digits from right to left. Let’s take 1011, for example.

Image description

Now we need to multiply the 1s and 0s with their matching powers of 2 and add them together, like so

Image description

So, 1011 in decimal is 11.

Addition of binary numbers
Adding binary numbers is similar to decimal addition, with some differences. Let’s add 1101 and 1001. We need to write them in rows like in traditional addition. We go from right to left and add the first ones using addition rules in the binary system. If we have a carry, we use it in the next column’s addition. Just like that, we can easily add binary numbers.

Image description

Image description

So, 1101 in decimal is 13, 1001 is 9, 13+9=22, and 10110 is 22, so our addition was correct.

Subtraction of binary numbers
To subtract binary numbers, we will also write them in rows. We can subtract binary numbers without a problem using simple binary subtraction rules. Let’s do 110-11.

Image description

Image description

We cannot 0-1, so we borrow a 1 and subtract using this logic. 110 in decimal is 6, and 11 is 3, so our subtraction was correct.

Multiplication of binary numbers

Multiplication of binary numbers is pretty simple; we write them in rows, take the second number’s first digit, and multiply it by every digit of the first number. Then we do the same with the other digits of the second number. Then we do the regular addition to get the result of our multiplication.

Image description

Image description

101 in decimal is 5, 11 is 3, and 1111 is 15, so our multiplication is correct.

Top comments (0)