DEV Community

Cover image for Binary Numbers: Introduction and Arithmetic Operations
yevastepanyan
yevastepanyan

Posted on

Binary Numbers: Introduction and Arithmetic Operations

What is Binary?

The word binary, in general, means something that involves two things. So Binary Number System is a system of numbers that has ... yes, two numbers. In other words, binary is a base-2 number system, which only uses two digits - 0 & 1.

In everyday life, we people use and understand the decimal system (0 to 9). You may ask that if people created computers, how come the computers only know two numbers instead of ten? The answer is straightforward. It turns out, guys, computers can only understand two states - whether there is electricity or there is not. 1 and 0 represent the presence or the absence of electricity, respectively. You know what that means, right?

Where do we use binary?

The binary system represents a number in terms of only two numbers, 0 and 1. A number in a decimal numeral system can be represented like

3654indecimal

Here, 10 is the base, as the number is represented using powers of 10. So if we want to represent the same number in a binary, we need powers of 2, t - logic.
Do you know how to convert the number 3654 from decimal to binary? Keep reading. After a few paragraphs, you'll get there. Maybe.

Converting from decimal to binary

As you already know, a binary number is an addition of powers of 2. But how do we exactly represent the number? Okay, first of all, let's pick a small number — 5, for example.

5

We've shown it as a sum. It's time to use our 1s and 0s. In the case of 5, it's 1*4 + 0*2 + 1*1. All we need is a number consisting of only 0s and 1s that expresses 1*4 + 0*2 + 1*1. Wow, we're definitely going somewhere with this. Look at the example of 3654. In the same way, in binary, it's

101

Let's take another example. 87 now. Now it's harder to find powers of 2; add them together and get 87. Here's an easy technique for that. Look at the first several powers of 2

Now, subtract the greatest power of 2 that is not greater than 87. It's 64.

87 - 64 = 23

Do the same thing with 23. It's 16 for 23.

23 - 16 = 7

Do this again,

7 - 4 = 3

And again...

3 - 2 = 1

And finally,

1 - 1 = 0

You got that

87 = 64 + 16 + 4 + 2 + 1

Starting from the right and considering the first place as the least power of 2 (the same as 1), we write 1 if there is a specific power of 2 present in the sum and 0 if not. To be more precise, here is a template.

Image description

Fill in the spaces, and you get

Image description

Indeed,

Image description

Congrats, now you know the conversion from decimal to binary. Conversion from binary to decimal is the same.

**

Operations with Binary Numbers

As in any number system, we can add, subtract, multiply and divide binary numbers. Let's take a look at each of the operations.

Addition

The addition of binary numbers takes place the same way as that of decimals... almost. In the case of binary, we use this table.

Image description

Not very clear, I know. You're probably wondering what the hell is 1+1=0 and carry 1. Let me explain with an example.

For instance, we try to add 1010 and 111.

1) Write the numbers in columns, same place digits in the same column like

Image description

2) Starting from the right column, add the numbers of each column.

2.1) For the first column, we have 0+1, which is equal to 1 according to our table.

2.2) Next comes 1+1. We know that 1+1 equals 0, and the carry is 1. So we write 0 and write a tiny 1 on the top of the next column.

2.3) The next step is adding 0+1, but oh wait, we also have a carry! Basically it's 1+(0+1) = 1+1 = 0 and 1 as a carry. AGAIN.

2.4) Finally, we have just a 1 in the last column. Should we just right it down? Nope. We forgot about the carry. This means we have 1 + 1, which is 0 (and a carry 1).

2.5) The only thing left is the last carry, which we right down.

Image description

Multiplication

Multiplication of binary is even simpler if you know addition.
Here's the table for multiplication

Image description

Again, let's write an example. 1001 x 111

1) Write the numbers in columns, just like during addition.

Image description

2) Multiply the second number's first (right) digit with the first number. If it is 1, copy the first number. If it is 0, write 0s.
In our example, it's 1, so we write 1001 in the first row.

Image description

3) Multiply the second digit of the second number with the first number. Write the result by moving 1 column left. So it's 0000 but starting from the next higher order digit like

Image description

4) Repeat the same action until the digits of the second number are over. The product obtained in each row is called the partial product.

5) Finally, add all the partial products. To add all the binary numbers, use the rules of binary addition.

Image description

Subtraction

Capture the table for subtraction

Image description

We're now doing subtraction using the same numbers in the multiplication example.

Guess the first step.
1) Write the numbers in columns

Image description

2) Start to subtract numbers starting from the right column and using the subtraction table rules.
2.1) For the first and second columns, 1-1 = 0 and 0-0 = 0
2.2) In the third column, we have 0-1, which equals 1, but we have to borrow a 1 from the next higher order digit.
2.3) We have nothing left in the last column because we have borrowed 1 from there.

Image description

Thank you for reading; I hope this was helpful. Have a great day!

Top comments (0)