DEV Community

Cover image for Binary Numbers
Razmik
Razmik

Posted on

Binary Numbers

Hello, my name is Razmik, and today I will talk about binary numbers, how they work, and about operations that you can do with them.
Binary numbers are a way of counting using just two digits: 0 and 1. In binary numbers, each digit represents a power of 2. Computers use binary because their internal language is based on on/off switches, where 0 is off and 1 is on, it is similar to electricity. This binary language allows computers to make all their operations.
Lets start from conversion, how to convert binary in decimal and vice versa.

For example you have number 17, lets convert it from decimal to binary.

17/2 = 8 with remainder 1,
8/2 = 4 with remainder 0,
4/2 = 2 with remainder 0,
2/2 = 1 with remainder 0,
1/2 = 0 with remainder 1.

Now, write down all the remainders from bottom to top to get the binary representation.

Lets convert the same number from binary to decimal.
Assign powers of 2 to each digit from right to left, starting with 0.
The number is 10001

2^0 * 1 = 1
2^1 * 0 = 0
2^2 * 0 = 0
2^3 * 0 = 0
2^4 * 1= 16

Multiply each digit in the binary number by the corresponding power of 2, and then sum up the results.
1 + 0 + 0 + 0 + 16
So, the decimal equivalent of the binary number 10001 is 17

Now lets check some binary operations:

Image description

Binary Addition:
Binary addition works much like decimal addition. We start from the rightmost bit and move to the left, carrying over when necessary. Remember, in binary, 1 + 1 equals 10 (just like how 1 + 1 equals 2 in decimal).
Here's an example:

Image description

Binary Subtraction:
Subtraction in binary is also similar to decimal subtraction. Borrowing occurs when the lower digit is larger than the upper digit. Here's an example:

Image description

Binary Multiplication:
Multiplying binary numbers follows the same principles as decimal multiplication. We multiply each bit of the first number by each bit of the second, shifting the second number to the left for each digit of the first number. Then, we add the results together. Here's an example:

Image description

Binary Division:
Binary division is a bit more complex, but the process is similar to decimal division. We repeatedly subtract the divisor from the dividend, shifting the quotient to the left each time until we can't subtract anymore. Here's an example:

Image description

Conclusion:
Binary arithmetic is essential in the world of computing, and understanding how to perform basic operations with binary numbers is fundamental for anyone interested in computer science or programming.See my other posts later on that page.

Top comments (0)