DEV Community

 Areg2004
Areg2004

Posted on

Binary Numbers

The word binary comes from "Bi-" meaning two. We see "bi-" in words such as "bicycle" (two wheels) or "binocular" (two eyes). A Binary Number is made up of only 0s and 1s. For example 10100, which is decimal number 20. Every number is possible to be converted to binary. We start from 0 then go to 1 then 0 again but add 1 on the left. For example, 0 is 0, 1 is 1, 2 is 10, 3 is 11 and so on. A single binary digit (like 0 or 1) is called a "bit". For example 11010 is five bits long.
Lets say we want to convert binary number to decimal. To begin with, we write down the binary number and list the powers of 2 from right to left. Increment the exponent by one for each power. Then stop when the amount of elements in the list is equal to the amount of digits in the binary number. For example, we have the binary number 1010. Starting from the right we get 2 squared 0 times 0 + 2 squared 1 times 1 + 2 squared 2 times 0 + 2 squared 3 times 1 and we get 10. Now lets say we want to do the opposite convert decimal to binary. We take decimal number as dividend. Divide this number by 2. Store the remainder in an array (it will be either 0 or 1 because of divisor 2). Repeat this two steps until the number is greater than zero.
We add binary numbers just like we add other numbers, from right to left. For example we want to add two binary numbers 110 and 101, we get 1011. The subtraction happens same way.
In order to multiple two binary numbers we have to keep in mind four things
0 × 1 = 0
1 × 0 = 0
1 × 1 = 1
0 × 0 = 0
We multiply each digit of the second number by the first whole number, then we just need to add them, switching each resulting multiplication one digit to the left. Division also follows the same process as decimal numbers.

Top comments (0)