DEV Community

Cover image for Binary representation of numbers
Taguhi Manukyan
Taguhi Manukyan

Posted on

Binary representation of numbers

Introduction to Binary

What will be equal to 2+5? You will obviously answer 7, but everything is relative and I will ask you the same question after you read this article.
There are many ways of communication, for example, the Morse alphabet, the Braille alphabet etc. Binary is a means of communication of people with computers.
In binary, we use two digits 0 and 1. With these two digits, we can represent all numbers, letters, sound waves, etc.
You will think, why do we have to do that? if we already have a standard 10-digit representation of numbers. The case is in computers. Computers use voltage to understand information; as voltage has two states on(1) and off(0), we use a two-state system.

Image description

Image description

Converting 10-base numbers to binary

Now the most interesting part!!! How we can convert 10-base numbers to 2-based?

We have to
1) divide the number by 2.
2) store the remainder.
3) divide by 2 the quotient of division.
4) continue the same until the quotient is 0.
5) then you have to take all remainders from bottom to top.
Exemples:
Image description
Image description
OR

We can
1) take powers of 2 and write them after each other
2)understand which power of 2 is the greatest power which is smaller than the number.
3) write 1 under that power of 2
4) subtract that power of 2 from the number
5)repeat this until the number is 0
6) write 0s under the powers of 2 which are not used.
7) start to write numbers from left to right from the first 1.
Examples:
Image description
Image description

Converting Binary to decimal

For converting binary to ordinary numbers we have to
1) write indexes of all digits of binary
2) raise 2 to the power of a particular index
3) multiply it by 1 or 0.
4) add them together
Example:
Image description

Image description

Addition of binary numbers

We do the addition of binary numbers as the ordinary addition for 10-based numbers.
1) we write numbers under each other
2) add digits from the same column together
3) if the sum of digits is greater than 1 we carry the whole part to the next left column.
Examples:
Image description

Image description

NOTE:
0+0 =0
1+0 = 1
0+1 = 1
1+1 = 0 and carry 1 to the next column
1+1+1 = 1 and carry 1 to the next column

Subtraction of binary numbers

For subtraction
1) We have to write numbers under each other
2)subtract digits from the same column
3) if the digit written first is smaller than the second we have to borrow 1 digit from the left column.
Examples:
Image description

Image description

NOTE:
1-1 = 0
0-0 = 0
1-0 = 1
0-1 = 10(with borrow)- 1 = 1

Multiplication of binary numbers

We do multiplication also as we do it for decimal numbers.
1)write numbers under each other
2)multiply each digit from the second number by the first number
3)add the numbers from the same column together.
Examples:
Image description

Image description
NOTE:
1*number = number
0*number = 0

Division of binary numbers

1)We have to take 1st digit of the number and divide it by the 2nd number
2) If the number which we want to divide is less than the second one we have to write 0
3)otherwise we have to write 1
4)Then we have to subtract the second number * 1 or 2 from the first number's digits
5) add another digit of the first number.
6)continue until you use all digits of the first number.
Examples:
Image description

Image description

Soooooo, what will be equal to 2+5? ;))

Top comments (0)