Binary representation of numbers
Tags: binary, numbers, representation, beginners
Published: 2022-09-15 17:24:10

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwg4t9flir5hiqqs8gvg.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yka0isfmffkfsvoho7h8.jpg)

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ax0dstgcdp69i6v5ca7.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b21c1pk4ftlivnvoyyq0.jpg)
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vh9jsz8kttybucl5brgj.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxjega2y1utq2un008fd.jpg)

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1swhzrh0pmbx6zt4d14u.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5m5tau1wsutis3r62yh.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fiu8dr1egv96jewu594.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0483bwei1oad3nidd9nk.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/062tjugkavsdvu2g2glf.jpeg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kyxirm9eyd95k3cwnz4f.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jte2fhvzy3alowlu55d.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvvrh40gnzz2sf36vkjw.jpg)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0xdpiaguxn1qnke2brs.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk7udmgxj47juifdtw28.jpg)

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