Binary and other numerical systems
Tags: binary, tutorial
Published: 2023-09-13 20:50:30

Computers use electricity to work, and electricity is turned on or off inside a microchip. That is represented by the numbers 1 and 0 and they are called binary numbers. Binary numbers are a base-2 numeral system used in mathematics and digital computing. Unlike our everyday decimal (base-10) system, which uses ten digits (0-9), the binary systems uses only two: 0 and 1. There are actually many numerical systems for example base3, base4, base5... base 16 and so on. And the concept of all these numerical systems is that whatever base it is it means that it has that number of digits (starting from 0). For example, in the case of base16 when the numbers end we start to write the alphabet. And we get this. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. An interesting fact is that out of all numerical systems humans chose base10, because they simply have 10 fingers (to count with). 

Firstly, let's understand how the decimal numbers work.

So, for counting numbers, there are things called "rules for counting" or "positional notation". But I think that the easiest way to understand how numerical systems work, is logic.

For decimal numbers, we have 10 digits, and when those 10 digits end (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), we just need to think of a number that contains the digits that we have and is the smallest number after 9. In this case, we need to use 1 and 0 and write 10. After that of course, the least one will be 11 and like that all the way to infinity.

The same applies to binary numbers. In this case, we only  have two digits 0 and 1. Again let's start thinking about the number coming next. Which number will come after 1 if we only have digits 0 and 1? Of course that will be 10. And like that, we'll get 0, 1, 10, 11, 100, 101, 110, 111 and so on. 

Moreover, binary digits are called bits and as we discussed they can have two possible values. There is a useful formula for counting the biggest number that can be represented in certain amount of bits by binary numbers. The formula is the following:

- 2^n-1, n is the number of bits

For example: What is the biggest number that can be represented in 5 bits?

2^5-1=31, which is the same as 11111, later we'll know how to do this manually, but for such cases the formula is very beneficial. 

Place Values: In binary, each place value represents a power of 2, just as in decimal where each place value represents a power of 10. For example, in the binary number 1101, each position represents a power of 2 as follows:

If we count from right to left for the number 1101, we'll get 0 1 2 3 it means that the places of the digits are the following 1-3, 1-2, 0-1, 1-0. Therefore we'll get:

- 2^3 (the leftmost digit)=8

- 2^2 (second from the left)=4

- 2^1 (third from the left)=2

- 2^0 (the rightmost digit)=1

This concept will be used for conversing binary numbers to decimal.

**Conversions**

To convert a binary number to decimal, you multiply each digit by its corresponding power of 2 and then sum the results. For example, to convert 1101 to decimal we need to do the following:

- (1*2^3)+(1*2^2)+(0*2^1)+(1*2^0)=8+4+0+1=13 (in decimal)

_Tip_: The concept is the same for converting any numerical system to decimal. The only difference is the number that we write the power of, in this case it was 2. For base4 it will 4, base5-5, base6-6... . You'll see such an example later in the blog.

We can also convert decimal to binary: To do that you need to repeatedly divide the decimal number by 2 and record the remainders until the quotient becomes 0. Reading the remainder from bottom to top gives you the binary representation. For example let's convert 825 to binary:



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

So now, if we read the remainders from bottom to the top we'll get 1100111001 which is the binary version of 825 in decimal. 

_Tip:_ If we're converting from decimal to any other numerical system, we should do the same thing for every case. The only difference is to use the right divisor. As we saw in the example above, in the case of converting decimal to binary we use number 2 as a divisor. Whichever base we are converting the decimal we need to divide it by the number of digits that the base has. Base two has two possible digits, that's why we divided the decimal by two. Now let's consider the case of converting base 10 number 27 to base4 using the same principle. 


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

So, we understood the concept of converting numbers from base 10 to any numerical system. There is also a _tip_ for converting binary numbers to a base which can be represented as a power of two. Such bases are, base4, base8, base16, base32, base64 and so on. Let's consider base8 and try to convert the binary number 110101111 to base8. As we know 8=2^**3**, that's why we need to group the binary digits into sets of three (if a group doesn't have 3 digits we just add 0's from the left). After that we need to convert the sets each individually to decimals. So we'll get:

- 110 = 2^2*1 + 2^1*1 + 2^0*0 = 4 + 2 = **6**

- 101 = 2^2*1 + 2^1*0 + 2^0*1 = 4 + 1 = **5**

- 111 = 2^2*1 + 2^1*1 + 2^0*1 = 4 + 2 + 1 = **7**

By collecting the numbers from top to bottom we'll get 657. If you want to check if the number is right or wrong just convert it to decimal and then to binary. That is the easiest way of converting random numerical systems if the three tips mentioned above don't imply. Let's check the 657 octal (base8 number). 

1. Converting to decimal

8^2*6 + 8^1*5 + 8^0*7=431

2. Converting to binary

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

By collecting the numbers from bottom to top we'll get 110101111 which is the same number we chose above. 

And one last example for this topic: We'll be converting base16 number 1A3 to base4. We need to do the same thing we did above.

1. Converting to decimal (taking the powers of 16, because we have base16)

Don't forget that A is 10 in decimal.

16^2*1 + 16^1*A + 16^0*3 = 419

2. Converting the decimal to base4. (divide repeatedly by 4)


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

By writing down the remainders from bottom to top we'll get 22031 which is the base4 equivalent of hexadecimal (base16) number 1A3.

If you want to know more about numerical systems and math operations with them, go ahead and watch my YouTube video.