DEV Community

Cover image for Binary Numbers (different number bases, conversions and mathematical operations)
Sona Grigoryan
Sona Grigoryan

Posted on

Binary Numbers (different number bases, conversions and mathematical operations)

Today we are going to learn about different number bases, binary numbers, conversation from one base to another one and also we will learn how to do mathematical operations with different base numbers.

I will also provide the link to my video about binary numbers.[Binary numbers video].(https://youtu.be/PbpHM63TCA8?si=wR9zLHoQ6iead2E3)

1) What are Binary Numbers?

Binary numbering system uses only two digits to represent numbers. Those digits are 0 and 1. Binary numbers are mostly used in programming and computer science, so learning binary numbers is very important in order to succeed in your profession. 1is considered the state when  electricity flows and 0 when no electricity flows. Imagine a box, and though that box electricity flows. And that box has to either let the electricity flow or block the way. So that is how we can get those 2 digits in computers. Those small boxes are called transistors and also bits ( places to store either 1 or 0). For example 1011 is a binary number and it is the same as 11 in our numbering system which is base ten or decimal. One more important thing to note. Based on the number of bits we have we can store up to different numbers in binary. For example, when we have 2 bits we the highest number possible to represent is 3 and the amount of numbers that can be expressed will be 4. The formula is the following.

The highest number: 2^n - 1
Amount of all possible numbers: 2^n

2) Different Base Numbers

So when I say base 2 or base 10 or base 16, etc, what does that mean? The number shows how many digits are used in that particular numbering system. For example, as we already said, binary or base 2 uses 2 digits, decimal or base 10 uses 10 digits 0-9. So you might ask what about base 16, for example? Well since there are not enough digits created to represent 16 different numbers, ifor base 16 and many other bases letter are used. So:

11 - A
12 - B
13 - C
14 - D
15 - E

As I said binary is used in computerers. Base 8 is also used. And base 16? Base 16 also has many usages, but probably the most important one is colors. To specify the color we use hex code or base 16 numbers.

3) other bases to base 10

I will only consider one example of base 2 to base 10, but other conversions are done by the same logic.

Let's take 11101 as our binary num.

So we write the number


11101

And  then assign ranks to them from right to left.

----------‐------------
4       3     2       1     0   

1    1    1    0    1

Then we take those rank and take 2 to the power of that number and multiply by the digit  and them all together.

----------‐------------

    1              1                 1             0             1
1×2^4  +  1×2^3 +  1×2^2 +  0×2^1 +  1×2^0


And after calculating we get 29 which is this binary number in decimal form.

The reason we rewrite that two is because we are converting from base 2 which has 2 digits. So If we were to convert from base 4 we would write 4 instead of that 2 and if from base 16, we would have written 16 instead of 2. And in base 16 since the form is with letters when following the formula we should take the corespondant number instead of the letter.numbers.

4) base 10 to other bases

Again I will only do the example on binary, but the principle is teh same and you can easily do the same for other bases.

Let's take 87 and concert it to binary.


87

The way to get tje binary number is to divide this number (87) by 2(because we want to convert to base 2) and get all the remainders which will construct our binary number. Now let's see on the example.

So we have 87

-‐-----------------------
87 | 1
43 |
‐-----------------------

1 here is the remainder and 43 is the result which we will gain devide by two the
Same way.

-‐-----------------------
87 | 1
43 | 1
21 | 1
10 | 0
5   | 1
2   | 0
1   | 1
0   |
‐-----------------------

Now so as you see we continue until we've reached 0. To get the binary number, we take those remainders from bottom to the top and there is our number.
In this example it is: 1010111 which is the binary representative of the 87.

5) operations with different bases

Operation with various bases works similarly to decimal numbers. Today I will only cover addition and multuplication for base 2 and base 4.

Addition

So let's do first binary.
Let's take 1011 + 11101

-‐----------------------‐-----------------------
01011
11101
-‐----------------------------------------------

As you see you can add 0 from the left end that new number will be equale to or number, the value won't change.

So let's add

‐----------------------‐-----------------------
01011
11101


         0          carry 1
-‐----------------------------------------------
       
First column  1 plus 1 we get 10 in binary so we write 0 and carry one over just like we do in decimals.

‐‐----------------------‐-----------------------
01011
11101


       00     carry 1
-‐----------------------------------------------

Second column got 1 + 0 = 1 but we also have 1 carried over so it becomes 1 + 1 again and so we write 0 and carry one over

‐‐----------------------‐-----------------------
01011
11101


    000     carry 1
-‐----------------------------------------------

Same thing in the third column as in the second.

‐‐----------------------‐-----------------------
01011
11101


  1000     carry 1
-‐----------------------------------------------

In the fourth column we already have three ones ( 2 initial and one carried over). So from first two ones we get 10 (write 0, carry 1 over) and then add  and get 1 in tat column.

‐‐----------------------‐-----------------------
01011
11101


101000    
-‐----------------------------------------------

In the next column we have the same thing one 1 and another 1 carried over so we get 10 and since there are no more columns we can write that 10.

Multiplication

Let's take 1101 × 1111

So again the same way as decimals.

‐‐----------------------‐-----------------------
               1101
               1111
             _______
               1101
            1101
          1101
       1101
   _____________

-‐----------------------------------------------

And then we add all of those together.

‐----------------------‐-----------------------
               1101
               1111
             _______
               1101
            1101
          1101
       1101
   _____________
                    11
-‐----------------------------------------------

---------------------‐-----------------------
               1101
               1111
             _______
               1101
            1101
          1101
       1101
   _____________
                 011      carry 1
-‐----------------------------------------------

---------------------‐-----------------------
               1101
               1111
             _______
               1101
            1101
          1101
       1101
   _____________
              0011  carry 1 over (×2)
-‐----------------------------------------------

---------------------‐-----------------------
               1101
               1111
             _______
               1101
            1101
          1101
       1101
   _____________
            00011  carry 1 over (×2)
-‐----------------------------------------------

---------------------‐-----------------------
               1101
               1111
             _______
               1101
            1101
          1101
       1101
   _____________
         000011  carry 1 over (×2)
-‐----------------------------------------------

---------------------‐-----------------------
               1101
               1111
             _______
               1101
            1101
          1101
       1101
   _____________
       110000011  carry 1 over (×2)
-‐----------------------------------------------

The addition works the same way as we wearied before so I believe you will be able to do the multilication.

Thank you for reading the entire post and I hope I was able to help you.

Top comments (0)