DEV Community

Davit Hayrapetyan
Davit Hayrapetyan

Posted on

Number Bases and Binary Operations

Hello everyone, today in this post I am going to cover such topics as binary numbers, different base numbers, conversion from one base to another one, and operations of binary numbers.

Table Of Contents:


Binary Numbers

So, why were binary numbers created, what purposes do they serve, how are they different from our usual numbering system, and their use in computers? Let's start. Think about the simplest way of communication, the one that can be used is when one asks a question and the other just responds yes or no. This is somewhat similar to the Boolean Algebra with only True and False values (True for yes and False for no). Then by using this concept we can communicate with computers. But how to implement it? For instance, we can use electricity (flow of electrons) for communication where the the flow of electrons means Yes (True) and its absence means No (False). For simplicity we denote these value with 1 (Yes) and 0 (No). So, here we go, this is the binary numbering system with base 2; it is called so because in this system we have only two digits 1 and 0. The numbers in binary system are different from the ones in our usual system (decimal or base 10) we use in everyday life. For example, 11011 in binary system equals to 27 in decimal system. Talking about computers, they store all of their information in these 1s and 0s, each in a special space allocated for just one digit which is called a bit. And to calculate the greatest possible number that can be represented using a certain number of bits, say with 8 bits, we use this formula, 2^n-1, where n is the number of bits. So, the highest value for 8 bits would be 2^8-1 = 256-1 = 255.
Additionally, the number of all possible numbers that can be represented in using n bits is calculated with this formula, 2^n; so, by using 8 bits we can represent a total of 2^8 = 256 numbers.


Different Base Numbers

The most common numbering system is the decimal one which has 10 digits (0,1,2,3,4,5,6,7,8,9), the reason it is the system humans have used for millennia is that the number of fingers we have on our hands, so it is base 10. However, there are other numbering systems with different bases, for example, base 4, base 8 (octal), base 16 (hexadecimal), base 3 etc. Take base 8, this means it has a total of 8 digits (0-7). For instance, the number 4735 in base 8 is 2525 in decimal. In case of base 16 we need 16 symbols to represent digits. We have 10 symbols from 0 to 9 for the other 6 it was decided to use letters from A to F (A,B,C,D,E,F), where we get:
10 -> A
11 -> B
12 -> C
13 -> D
14 -> E
15 -> F.
So, for example F5AB7 in hexadecimal is 1,006,263 in base 10. We will discuss how we got this number just right now.


Conversion Between Bases

But how do we convert a number of one base so that to get the same number in another base? There are two main cases and one additional. The main ones are conversion from other bases to decimal and vice versa, from decimal to others.


Conversion To Decimal

Well, to understand the principle let's first understand how a number is represented in base 10. Take the number 48,315. We can represent it with the sum of products of the digits and 10 to the power of the index of a particular digit, where the index of a digit is its order, starting from 0, from the right to the left of the number. At first this is hard to understand but here is the method which is quite easy:

Index 4 3 2 1 0
Number 4 8 3 1 5

According to this, the indexes of the digits are 5->0, 1->1, 3->2, 8->3, 4->4. So, using this we can write it as the following sum:

4*10^4 + 8*10^3 + 3*10^2 + 1*10^1 + 5*10^0 =
= 40,000 + 8,000 + 300 + 10 + 5 = 48,315

The same way is for other numbering systems, take for example 101101 in binary and convert it into decimal. First we need to numerate the digits by their indexes:

Index 5 4 3 2 1 0
Number 1 0 1 1 0 1

Here we repeat the same but with the powers of 2. So we should get:

1*2^5 + 0*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0 =
32 + 0 + 8 + 4 + 0 + 1 = 45

So, 101101 is 45 in base 10.

Let's now convert from base 5 to decimal. Take number 4301 in base 5. The first step is to numerate the digits:

Index 3 2 1 0
Number 4 3 0 1

Then write the sum of powers of 5:

4*5^3 + 3*5^2 + 0*5^1 + 1*5^0 =
500 + 75 + 0 + 1 = 576

And for the last one let's convert F6AB1 from base 16 to base 10. Here there is a some tricky part because we can not multiply the power of 16 to letter F. In this case we need to have in our minds the list we mentioned above:
10 -> A
11 -> B
12 -> C
13 -> D
14 -> E
15 -> F
So we just need to replace the letter with the respective number. The steps are the following:

Index 4 3 2 1 0
Number F 6 A B 1

"F"16^4 + 6*16^3 + "A"*16^2 + "B"*16^1 + 1*16^0 =
15*16^4 + 6*16^3 + 10*16^2 + 11*16^1 + 1*16^0 =
983040 + 24576 + 2560 + 176 + 16 = **1,010,368
*

So, we have got this number, it is pretty big but this is good to understand how all the parts of the conversion work.


Conversion From Decimal

To convert decimal number to other bases we need to divide it with the base of the number which we should get and take the remainders of the divisions in the opposite order , let's see how it is done. To convert 137 to binary we divide 137 by 2 as many times as possible by using the following formula r + b * q = a, where a is the dividend, b is the divisor, q is the quotient, and r is the remainder

^ 1 + 2 * 68 = 137
|| 0 + 2 * 34 = 68
|| 0 + 2 * 17 = 34
|| 1 + 2 * 8 = 17
|| 0 + 2 * 4 = 8
|| 0 + 2 * 2 = 4
|| 0 + 2 * 1 = 2
|| 1 + 2 * 0 = 1

Note that we divide the number until we get 0 in the quotient. So, the result number is 10001001. You can check its validity yourself by the method we described a little earlier.

Let's convert 627 from decimal to base 7. First, we divide the number by the base, which is 7, as many times as possible:

^ 4 + 7 * 89 = 627
|| 5 + 7 * 12 = 89
|| 5 + 7 * 1 = 12
|| 1 + 7 * 0 = 1

So, if we take the remainders in a reversed order we get 1554 in base 7 which is exactly the same as 627 in decimal. You can also check it by yourself.

To also cover the case where we use letters let's convert 2655 from decimal to hexadecimal. You already know the sequence of steps but this time we need to have the table of number equivalents of letters in our mind (that is A->10, B->11, ..., F->15):

^ F <= 15 + 16 * 165 = 2655
|| 5 + 16 * 10 = 165
|| A <= 10 + 16 * 0 = 10

The result is A5F.


Grouping Method

You might think of how to convert a number from any base directly to another, say from 5 to 13. The thing is that it is not possible to do it. The only way is to convert to decimal and then to any desired base. However, there is a way of doing it when you want to convert a number from aby base M to base N if and only if N is a power of M, that is M^x = N, where x is the power of M. Hence, we can convert binary to base 4, 8, 16, 32 etc. base 3 to base 9, 27 etc. Here is the way we do it. Let's convert the number 1100111 to base 4. Firstly, we divide the number into groups of x, where x is the power of 2 in term of which the result is 4, that is x = 2. Also it may be necessary to add a 0 to the left of the number to form a whole group because the zero on the left of the binary number does not change anything and can be neglected.
So, the grouped number will look like this |01|10|01|11|. Then we need to convert each group separately to decimal and concatenate the results.

First group:
01 -> 0*2^1 + 1*2^0 = 1

Second group:
10 -> 1*2^1 + 0*2^0 = 2

Third group:
01 -> 0*2^1 + 1*2^0 = 1

Fourth group:
11 -> 1*2^1 + 1*2^0 = 3

So the number is 1213 in base 4. You can check it yourself by converting 1100111 to decimal and then to base 4.

The same way is when we are converting binary to hexadecimal. Let's do this for 110101100. First we group it now into groups of four digits and we get |0001|1010|1100| (see we added three 0's to the left of the number, because the digit 1 on the left would be the single one in the group). Then converting groups but keeping in mind the number equivalents of the letters in hexadecimal system:

First group:
0001 -> 0*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 1

Second group:
1010 -> 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 10 -> A

First group:
1100 -> 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0 = 12 -> C

The output is 1AC.


Operations With Binary Numbers

As with decimal numbers, we have operations of addition, subtraction, division, multiplication, and also negative numbers (2's complement) in binary system, too. We are going to cover all of them.


Addition

Fundamentally it is the same as with decimals but we need to understand the principles. Here are the rules table for each case, where A and B are the numbers we need to add to, C-in (carry in) is the carry that was produced in the previous operation that also needs to be calculated in the particular pair of the numbers, and the C-out (carry out) is the carry that is output (which comes out when the sum is greater than 1 and we get a two digit number such as 10 or 11):

A B C-in Sum C-out
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1

So according to this table let's add 101101 to 110111:

 101101
+110111
------- 
      0 carry-out(1)
Enter fullscreen mode Exit fullscreen mode

1 + 1 = 10 (0 - sum, 1 - carry out)

 101101
+110111
------- 
     00 carry-out(1)
Enter fullscreen mode Exit fullscreen mode

0 + 1 + 1(carry in) = 10 (0 - sum, 1 - carry out)


 101101
+110111
------- 
    100 carry-out(1)
Enter fullscreen mode Exit fullscreen mode

1 + 1 + 1(carry in) = 11 (1 - sum, 1 - carry out)


 101101
+110111
------- 
   0100 carry-out(1)
Enter fullscreen mode Exit fullscreen mode

1 + 0 + 1(carry in) = 10 (0 - sum, 1 - carry out)


 101101
+110111
------- 
  00100 carry-out(1)
Enter fullscreen mode Exit fullscreen mode

0 + 1 + 1(carry in) = 10 (0 - sum, 1 - carry out)


 101101
+110111
------- 
1100100 
Enter fullscreen mode Exit fullscreen mode

1 + 1 + 1(carry in) = 11 (0 - sum, Here we just write the num as it is because this is the last operation)

So the sum is 1100100.


Subtraction

As with addition the principles are the same as with decimals. Here, there is a nuance it is the borrowing when the digit that is subtracted from is less than the subtractor digit we need to take ("borrow") the value of the following digit which is equal to 10 in binary (2 in decimal) that is two 1s. Here are the cases of subtraction:

  • 0 - 0 = 0
  • 0 - 1 = 1 (borrow 1)
  • 1 - 0 = 1
  • 1 - 1 = 0

Example of Borrowing:

To subtract 1 from 0:

  1. Borrow from the next left bit.
  2. Change the current 0 to 10 (which is 2 in decimal).
  3. Perform the subtraction.

Let's examine the example of 1011 and 0110

  1011
- 0110
Enter fullscreen mode Exit fullscreen mode

Rightmost Column (1st): 1 − 0 = 1
Next Column (2nd): 1 - 1 = 0
Next Column (3rd): 0 - 1 (borrow from left): becomes 10 - 1 = 1
Leftmost Column (4h): (was borrowed from): becomes 0 - 0 = 0

  1011
- 0110
-------
  0101
Enter fullscreen mode Exit fullscreen mode

The answer is 0101.

Now do another more complex example with multiple borrows. Let's subtract 00111 from 10010:

  10010
- 00111
Enter fullscreen mode Exit fullscreen mode

Rightmost Column (1st): 0 − 1 (borrow from left): becomes 10 - 1 = 1
Next Column (2nd): (was borrowed from): becomes 0 - 1

Here we need to borrow but the left digit is 0 so we need to borrow from the nearest one like a cascade, so after the borrow and subtraction we did above the number will look like 10000 and we borrow from the nearest possible digit that is the leftmost one. The cascade of borrows will look like this:

10000 -> 0{10}000 -> 01{10}00 -> 011{10}0
Enter fullscreen mode Exit fullscreen mode

So after all these borrows we get:

  011{10}0
- 001  1 0
Enter fullscreen mode Exit fullscreen mode

Proceeding the operations from the second column:

(Rightmost Column (1st): 0 − 1 (borrow from left): becomes 10 - 1 = 1)
Next Column (2nd): (after borrows) 10 - 1 = 1
Next Column (3rd): 1 - 1 = 0
Next Column (4th): 1 - 0 = 1
Next Column (5th): 0 - 0 = 0

The outcome is *01011


Multiplication

For multiplication it is essentially the same as with decimals or somewhat even easier. The first step is to multiply each digit of a factor with the other factor, write it down, and for the same for the next digit of the first factor but write down the product one bit left, and eventually do the ordinary addition. Let's multiply 11 with 10:

   11
 * 10 
 -----
   00
Enter fullscreen mode Exit fullscreen mode

Here we only multiplied 0 with 11, the next step is multiplying 1 with 11 but writing it one bit left. It will look like this:

    11
  * 10 
  -----
    00
 + 11
 ------
   110
Enter fullscreen mode Exit fullscreen mode

The sum is 110.

Let's do more complex multiplication of 1101 and 101:

    1101
  *  101
  ------
    1101
   0000
+ 1101
--------
 1000001
Enter fullscreen mode Exit fullscreen mode

The product is 1000001.


Division

Division is implemented using both multiplication and subtraction first we consider the divisor and the leftmost digits of the dividend with the same number of digits as the divisor. Write down the biggest number in the quotient for which its product with the divisor will be less than the leftmost digits of the dividend. Then bring down the next digit of the dividend, and repeat the same steps till the last digit of the dividend.

Let's see how it is done on an example of 11010 and 11:

dividend
   ^
   |
  11010|11 -> divisor
       ----
       |   -> quotient
Enter fullscreen mode Exit fullscreen mode

First step:

  11010|11 
- 11   ----
 ---   |1
   0
Enter fullscreen mode Exit fullscreen mode

Second step (00 is less than 11 so we just add 0 to the quotient):

  11010|11 
- 11   ----
 ---   |10
   00
Enter fullscreen mode Exit fullscreen mode

Third and fourth steps:

  11010|11 
- 11   ----
 ---   |1000
   0010
Enter fullscreen mode Exit fullscreen mode

The quotient is 1000 the remainder is 10

2's Complement Method

To get the negative of the binary number we need to reserve a bit for the sign (0 when positive, and 1 when negative). To understand the principles it is best to see it on an example. Let's negate 1011:

As 1011 is positive, the sign bit is 0, the num is 01011, then we need to write the inverse of the number that is:

01011
-----
10100
Enter fullscreen mode Exit fullscreen mode

Here, at this moment it is 1's complement, then we add 1 to the inversed number:

  01011
  -----
  10100
+     1
-------
  10101
Enter fullscreen mode Exit fullscreen mode

The negative of 1011 is 10101, but note that the leftmost bit is the sign notation. To check if we have got the correct number do the following:

Index 3 2 1 0
Number 1 0 1 1

Calculate the decimal of 1011:
1*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = 1 + 2 + 8 = 11

Index 4 3 2 1 0
Number 1 0 1 0 1

Convert it to decimal 1*2^0 + 0*2^1 + 1*2^2 + 0*2^3 - 1*2^4 (the sign bit which is negative) = 1 + 4 - 16 = -11

See the output is the negative of the initial number. This is the 2's Complement method.

Thanks to everyone who have read to this point, hope this post was helpful.

Top comments (0)