DEV Community

Cover image for Binary representation and operations on binary numbers
anetasargsyan
anetasargsyan

Posted on

Binary representation and operations on binary numbers

What is binary?

  • Binary is a base-2 numbering system that has two states (“0”, “1”).

Why do we use binary?

Computers can understand only 2 states: either there is electricity or there is not => 0 / 1.

Understanding binary representation of a decimal number.

The Decimal system is base-10. To represent a number of base-10 system we use our base, and add power to it depending on which row it is on (counting from right to left, starting with 0)

589 represented as decimal

Since the binary system is base-2, we do the same just changing the “10” by “2”.

21 represented as binary

(this is 21 in decimal)

Conversion from decimal to binary and vice versa

Converting from binary to decimal is what’s shown above: If you calculate the right-hand side, you’ll get 21 which was represented in binary.
Now let’s get to conversion from decimal to binary:
To convert it we need to divide that number by two, and every time write the quotient until you get either 1 or 0. Also, remember to write the remainders right by their side. I’ll give an example of 21. Let’s convert it.

Binary representation 21 division method

So, we divide 21 by 2, get 10 and 1 as quotient, write it down, and continue. Now we have 10 left: we divide that by 2, get 5, and 0 remainder, write that down and repeat again. Now we have 5: divide that by 2, get 2, and 1 as a remainder. Finally, we divide 2 by two, get 1 for the quotient, and 0 for the remainder. Now we need to “assemble” our binary number. We start from the very end: the last quotient - 1, we write all of the remainders from down-up. So we go in the direction of the purple arrow. I wrote the remainders top-down, so we need to reverse it. Read it down-up, and you will get 10101, which is, in fact, 21 in decimal.

Addition of binary numbers

Arithmetic operations we know work for binary numbers as well. They might seem slightly different, but mostly it is the same, just it is a bit unusual for our eyes) Let’s dive into addition.

To add numbers we can use the traditional method of writing one below the other and add rows with carrying any additionals to the next row. Let’s add 11 to 10 to get 21 again.

10 represented as the binary number will be - 1010,
And 11 will be - 1011)

Binary addition of 10 + 11

So, we got 10101, which is, indeed, 21)

Multiplication of binary numbers

Multiplication works the same way we are used to: write the numbers we need to multiply one below the other, multiply the rightmost digit of the multiplier with all the digits of the multiplicand. Add a placeholder of '0' before multiplying the next higher order digit of the multiplier with the multiplicand's digits. Repeat the same process for all the following digits. Then add the products to get the outcome of the multiplication.

Binary Multiplication

P.S. I'll do another post featuring subtraction and division of binary numbers :D

Top comments (1)

Collapse
 
artakgevorgyandev profile image
Artak-Gevorgyan-dev

WOW, really cool article.