DEV Community

Mane Khachatryan
Mane Khachatryan

Posted on

Binary numbers, other number representations, conversion, and operations of binary numbers. Mane Khachatryan

Binary numbers are the base of the computer. They consist of two digits- 0 and 1. Those digits are stored in bits, which are the lowest units of data. Binary numbers are mainly used in computers, and they can represent information in digital formats.
It is easy to convert decimal numbers into binary. To do so, we need to divide the decimal number by two; if we get a remainder, we write 1; if we get no remainder, we write 0. We need to repeat these steps until the quotient is 1. Afterward, we need to write the number from bottom to top.
For example 12 in binary will be 12|0
6|0
3|1
1|1, 12=1100
To convert binary to decimals, we need to number the bits from right to left. Then, we need to write the powers of 2 according to the number of bits and multiply by the number in the bit.
1100= 2^0*0 + 2^1*0+ 2^2*1 + 2^3*1= 0+0+4+8=12

We use binary numbers in computing, but there are other number representations as well. For example, we use base 10, which are decimals; base 16, which is widely used for coding different colors; base 8 and base 4 are less common but are still used in programming. It is easy to convert those numbers, too. Let us take base 4. Instead of 2 digits, we have 4 digits here- 0,1,2,3, so basically, we can do the same steps when we convert decimals to binary, but instead of dividing the number by 2, we need to divide the number by 4, and when we want to convert base 4 to base 10, starting rightmost we write the powers of 4 and then multiply by the number written in bits.

If we want to calculate the biggest number that can be written in n digits, we use the formula (2^n)-1. For example, if we have 8 bits, the largest number that can be represented in them is (2^8)-1=255.

As decimals, binary, and other number representations have operations. Let's discuss the operations of binary numbers.
To add binary numbers, we need to add the corresponding digits together.
0+0=0
0+1=1
1+0=1
1+1=0, and we carry the 1 over.
While subtracting, we have the following rules
0-0=0
1-0=1
10-1; in this case we take two's complement and write 2-1
For example 1001
- 100
= 101
Multiplication is similar to decimal numbers.
0*0=0
1*0=0
0*1=0
1*1=1
After multiplying each row by the rightmost bit, we must add those numbers in order to get the final result.
For division, we have the following rules:
0/1=0
1/1=1
Dividing by 0 is meaningless.

Binary and other number representations are important to understand how computers work and are imperative to understand the basics of programming.

Top comments (0)