DEV Community

Maria Nazaryan
Maria Nazaryan

Posted on

BINARY NUMBERS

Image description


What are Binary numbers?

All Binary numbers are made up of only 0s and 1s.
1101101
This is a binary number example.
As you understood there are no 2, 3, 4, 5, 6, 7, 8 or 9 in Binary numbers. Binary numbering systems are best suited to the digital signal coding of binary, as it uses only two digits, one and zero, to form different figures.

How do we count using Binary numbers?

The first digit is 0. Then 1. Then there is no 2 in Binary representation of numbers. So, what we do? We write number 0 and add 1 from front. So, we get number 10(one and zero) as number 2. Then we write 1 and 1 together: 11 as number 3. (Below I will explain how we convert “normal” numbers to binary.).
Here is the table of some equivalent values.

Image description

Image description

How to Convert Decimal to Binary?
Option 1.

We us only the powers of two.(2^0 is two power of zero)
2^0,2^1, 2^2,2^3,2^4,2^5,2^6,2^7,2^8...
Which are equal to 1,2,4,8,16,32,64,128...
So, for example we have a number 85 and we want to make it Binary.
We look at the numbers of powers of two. The number that is least small than 85 is 64.
So, we write 85=64+16+4+1
85-64=21
Then the next number that is smaller than 21 is 16. We do the same thing:
21-16=5
Then
5-4=1
1-1=0
As you already understood I wrote 85=64+16+4+1 using all the numbers of power of two that we used.
So, there was this sequence
1,2,4,8,16,32,64,128
So, under the numbers till 64 we should write 1 if we used them and we should write 0 if we didn’t.
1, 2, 4, 8, 16, 32, 64
1 0 1 0 1 0 1
We get that 85 in Binary representation is 1010101.

Option two.
This option is by division.
Lets take again 85.

Image description

So, you see all the reminders, right? They are only 1s and 0s.
So, we take the reminders and put in opposite direction from bellow to above.
We get 1010101. It is the binary representation of number 85.

Practice yourself:
Convert numbers 96 and 105 into binary in what kind of option do you like.

How to convert Binary to Decimal?
Let’s take the previous example: 1010101, 85. And let’s prove that 1010101 is 85.
At first, we write the binary number:
1 0 1 0 1 0 1
Then from right side to left we right the powers of 2: (We start from 2^0)

Image description

Then we multiple the powers of 2s by the number (1 or 0) written above and then add them to each other.
So, the number will be:

Image description

So, we understood once more that 1010101 binary number is 85 as decimal.

How to add and subtract binary numbers.

Addition

Indeed, how to add binary numbers? We are experts at adding decimal numbers. Do binary numbers’ addition work with the same principal?
Let’s start with the easiest one:
0+0, nothing plus nothing is NOTHING, so 0+0=0
1+0, something plus nothing is obviously something, so 1+0=1
Then, the little tricky one:
what is 1+1? Yes, it is 2, however it is not binary it is decimal. We should write 2 in binary which is 10. (Check it if you want by the method we learned in previous section).
Let’s think like this. 1+1, we write 0 in the right side and then we write 1 in the left side as it was our carry.
What is 1+1+1. Obviously, 1+1 is ten. Then 10+1 is 11. Great! That means 1+1+1 is 11.
These were the main rules of addition, so let’s understand it deeply with example.
10010+ 11010=?
Let’s first write 10010 and then write 1110 under it (to make it less complicated).

Image description

Explanation: So, from the right side 0+0=0(that’s why we write 0 from the right in the sum). Then, 1+1 is 0 and carry 1. We right only 0 and carry 1. (Don’t forget about this carry, it is important). 0+0 is 0, however we carried 1, so the third member from the right side in sum should be 1. Then 1+0 is 1. And in the end, 1+1 is 10.
If you want, you can check it by converting this numbers into decimal. Their sum should be equal to 101100 in binary representation.

Subtraction:

1) What is 0-0, nothing minus nothing is nothing, so 0-0 =0
2) What is 1-0, something minus nothing is something, so 1-0 =1

Lets have an example.
What is 10-1?

Image description

Explanation: We have 0-1 which is a negative number, so we “borrow” one. from 0’s neighbor, which is 1. If we borrow it, our 0 will become 2, and there will stay nothing (basically 0) in the right part. So, 2-1 =1. This means 10-1=1

Another example:

Image description

Explanation: First, 1-1 is 0, so we write 0 in the end. Then 0-0 is 0. Then we have 0-1, which is a negative number, so we “borrow” 1 from the neighbor. Our 0 becomes 2 and the neighbor becomes nothing. (0). 2-1 =1.
So, 1001-101=100

Multiplication of binary numbers

Multiplication is going to be much easier when you already know addition of binary numbers.
Lets take and example and understand how we multiply binary numbers.
Let’s multiply 1011 and 101.

Image description

Explanation: We do multiplication in a normal way as we do it with decimal numbers. 1x1=1, 1x0=0, 0x1=0,0x0=0. So, from the right we multiply 1001 with 1. It will be 1001. (Make sure you write every next step by passing the last digit’s place (there is nothing, a loop)). Then we take the next digit:0. We multiple 0 with 1001, which will be 0000. The next one is again 1. 1x1001 is 1001. IN THE END we do some addition. We add the numbers from the multiplication. In the loops there are zeros, you can write it if you want, it is not mandatory. So, we get 101101. (You can check it if you want.)

**Division of Binary numbers:**

Do we all remember the division of decimal numbers?
For example:
144:9

Image description

First, there is 1, which is smaller than 9, so we take 14, which contains 9 in it. There is only one 9 in 14, so we write 1 and subtract 9 from 14. It will be 5. An then we write 4 near 5 and we get 54. There are six 9s in 54, so we get 144/9=16.

Basically, we do the same thing with binary numbers, and if you already know the subtraction of binary numbers it will be much easier for you to understand division.
Let’s take the number 101010 and divide it with 110.

Image description

Explanation: So, we take 1010, because 1<110, 10<110,101<110. We can multiply 110 by 1 or by 0. So, we obviously multiply 110 with 1. 1x110 is 110. Then, we subtract 110 from 1010. 0-0=0,1-1=0. There is 0-1 which Is not possible, so we carry 1 from the 1 in the left side. That 1 becomes nothing (0), our 0 becomes 2(as you remember from the subtraction rules.) And we have 2-1=1. So, there is 100, which is smaller than 110. We take another one above and write it near 100. It will become 1001. Again, we do the same thing, we multiply 110 with one and subtract the result (110) from 1001. This time there are two zeros, so we take carries for two times. There will be 121-110, which will be 011. Then we take the last 0, and write near 011(11, we ignore the 0). We get 110. We multiply 110 with one, get 110 and subtract it from 110. 110-110=0. This was a division without reminder. There can be reminder which would be < than 110(in this case.)

Top comments (0)