Number representation systems. Binary operations
Tags: binary, beginners, programming, number
Published: 2023-09-14 00:45:47

In this post I will explain common number representation systems, how to convert numbers between them and main operations(addition, subtraction, multiplication) of that numbers.

**What is the most used number representation system?**

Decimal numbers, also known as base-10 numbers, are the numbers we use in our everyday life and mathematics. The decimal numeral system is based on powers of 10. In the decimal system, there are ten distinct digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Each digit's place value in a decimal number corresponds to a power of 10. Starting from the rightmost digit, the place values increase by a power of 10 for each position to the left. Here's a breakdown of the place values:

- 1st position to the right: 10^0 = 1
- 2nd position to the right: 10^1 = 10
- 3rd position to the right: 10^2 = 100
- 4th position to the right: 10^3 = 1,000
- ...

Decimal numbers are the most common way to represent numbers in everyday life, making them a fundamental part of mathematics and our numerical system.

**Why we need other systems?**

In binary, numbers are written using these two digits, and they are used extensively in computer science and digital electronics because the easiest way to communicate with machines is to make electricity flow through them or stop the flow. Binary is the foundational language of computers, where electrical signals are either "on" (1) or "off" (0).
Unlike our everyday decimal system, which uses ten digits (0-9), binary uses only two digits(0, 1). Each digit in a binary number represents a power of 2, with the rightmost digit representing 2^0 (1), the next digit to the left representing 2^1 (2), the next 2^2 (4), etc.

- 0 in binary is 0 (2^0 * 0 = 0)
- 1 in binary is 1 (2^0 * 1 = 1)
- 10 in binary is 2 (2^1 * 1 + 2^0 * 0 = 2)
- 11 in binary is 3 (2^1 * 1 + 2^0 * 1 = 3)
- 100 in binary is 4 (2^2 * 1 + 2^1 * 0 + 2^0 * 0 = 4)

Binary numbers are fundamental in computer programming, data representation, and digital communication, making them an essential concept in technology and computing.

**How to convert numbers from decimal to binary?**

Converting a decimal number to binary is a straightforward process. You can follow these steps:

1. Divide the decimal number by 2.
2. Record the remainder (which will be either 0 or 1).
3. Repeat the process with the quotient from the previous step until the quotient becomes 0.
4. Read the binary equivalent by arranging the remainders in reverse order (from bottom to top).
Here's an example to illustrate the process:

Let's convert the decimal number 42 to binary:

1. 42 ÷ 2 = 21 (with a remainder of 0). Record the remainder: 0.
2. 21 ÷ 2 = 10 (with a remainder of 1). Record the remainder: 1.
3. 10 ÷ 2 = 5 (with a remainder of 0). Record the remainder: 0.
4. 5 ÷ 2 = 2 (with a remainder of 1). Record the remainder: 1.
5. 2 ÷ 2 = 1 (with a remainder of 0). Record the remainder: 0.
6. 1 ÷ 2 = 0 (with a remainder of 1). Record the remainder: 1.

Now, read the remainders from bottom to top: 101010. So, the binary representation of the decimal number 42 is 101010.

**How to convert numbers from binary to decimal?**

Converting a binary number to decimal is a bit different process. Here are the steps:

1. Start from the rightmost digit (the least significant bit) of the binary number.
2. Assign a place value of 2^0 to the rightmost digit.
3. For each digit to the left, double the previous place value.
4. Multiply each digit by its corresponding place value.
5. Sum up the results of these multiplications.

Here's an example to illustrate the process:
Let's convert the binary number 101010 to decimal:

1. Start from the rightmost digit (0 in this case).
2. Assign a place value of 2^0 = 1 to the rightmost digit (0), so it contributes 0 to the total.
3. Move to the next digit (1).
4. Double the previous place value: 2^1 = 2.
5. Multiply the digit (1) by its corresponding place value (2): 1 * 2 = 2.
6. Add this result to the running total: 0 + 2 = 2.

Repeat the process for the remaining digits:

- Next digit (0): Double the previous place value: 2^2 = 4. Multiply the digit (0) by 4: 0 * 4 = 0.
- Next digit (1): Double the previous place value: 2^3 = 8. Multiply the digit (1) by 8: 1 * 8 = 8.
- Next digit (0): Double the previous place value: 2^4 = 16. Multiply the digit (0) by 16: 0 * 16 = 0.

Now, sum up all these results:
2 + 0 + 8 + 0 = 10.
So, the binary number 101010 is equal to the decimal number 10.

**How to add binary numbers?**

We add binary numbers with the same method for decimal numbers. We write them under each other and add the digits from the same column. We start from the digit at the very right and then go to the left. The only case of carry is if the sum of digits is greater than 1. In that case we carry the whole part to the next left column. 
You can see an example of addition below.

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

**How to subtract binary numbers?**

For subtraction we also write numbers under each other and then we substract the digits from the same column. We start from the digit at the very right and then go to the left. If the digit written on upper side is smaller than the lower one we have to borrow 1 digit  from the left column.
You can see an example of subtraction below.

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

**How to multiply binary numbers?**

The principle of multiplication is also same. We multiply each digit from the second number by the first number each time writing the product one digit to the left and then summing all the product together.
You can see an example of multiplication below.

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

**Thank you for reading my post, I hope it was useful.**