GET EDUCATE

Converting one base system to another

In this tutorial, we will discuss how we convert one base system to another. We will discuss the following.

  1. Decimal to Binary
  2. Binary to Decimal
  3. Decimal to Octal
  4. Octal to Decimal
  5. Decimal to Hexa-decimal
  6. Hexa-decimal to Decimal
  7. Binary to Octal
  8. Octal to Binary
  9. Binary to Hexa-decimal
  10. Hexa-decimal to Binary

You may refer the following YouTube video for converting decimal to binary and vice-versa. 

Decimal to Binary conversion

To convert decimal number to binary we repeatedly divide the number by 2, till we get the quotient as 0. The binary number is base – 2 system, we can only use the digits 0 and 1.

Ex : N = 25, to convert 25, we divide 25 repeatedly by 2. Then we write the remainders in bottom-up manner.

decimal to binary

2 * 12 = 24, 1 is the remainder

2 * 6 = 12, is the remainder

2 * 3= 6, 0 is the remainder

2 * 1 = 2, 1 is the remainder

2 * 0 = 2, 1 is the remainder

Therefore,

 (25)10 = (11001)2 

 

 


 

Binary to Decimal conversion

We write 123 as,

1 * 102 + 2* 101 + 3*100

Since the number 123 is in base 10 system we multiply it with 10 powers. Similarly, to convert from binary to decimal we multiply it with 2 powers.

Let us consider the binary number 11001. We will convert this to decimal.

First, we write 11001 in placeholder form as,

4 3 2 1
1
1
1

Now, we multiply, the digits with the 2 powers of its placeholders.

Therefore,

(11001)2 = 1*24 + 1*23 + 0*22 + 0*21 + 1*20

= 16 + 8 + 0 + 0 + 1

= 25

We have only seen how we convert the positive numbers to binary. Let us see how we convert negative numbers to binary. Negative numbers are stored in 2’s complement form.

We know that integer in C programming language takes 4 bytes (its compiler dependent, some compiler takes 2 bytes).

In GCC compiler, integer takes 4 bytes. That means the maximum possible value that we can store in integer is 231 -1. Which is also known as INT_MAX (defined in limits.h) and the minimum value that we can store in integer is -231 . Which is also known as INT_MIN (defined in limits.h)

How to calculate 2's complement

Before we calculate the 2’s complement let us understand how integers are represented internally in memory.

integer number representation

Integer takes 4bytes = 32 bits. We have named the bit number from 0 to 31. Each cell can be filled in two possible ways with 0 /1. But the 31st bit (the MSB – Most significant bit) is reserved bit to represent if the number is positive or negative. If the MSB is 0, then number is positive, is MSB is 1, then number is negative. Therefore, we have only 31 bits to decide magnitude and 1 bit for sign. Hence the maximum number can be 231 -1 . (as 0 is also counted and each cell can be filled in 2 possible ways)

To calculate 2’s complement we take 1’s complement and add one to it. 

Calculating 1's complement

We flip all the bits (make 0’s as 1 and 1’s as 0) to get the 1’s complement of the number. After we get 1’s complement we add 1 to it.

Before that, it is good to know the binary addition.

   0                                   0                                1

+ 0                               + 1                             + 1

———                     ———–                 —————–

0                                      1                                 10

——–                     ————–                ——————-

 

Since 1+1 = 2, but we cannot use 2 in binary system, we write 0, and carry forward the 1. Therefore, 1+1 = 10 in binary.

Let us calculate 1’s complement of N = 25. First, we convert 25 to binary and write in 32-bit format with sign bit being 0.

number in bit format

We flip all the bits including the reserved bit. 

Therefore, 1’s complement of 25 is

1's complement

To this 1’s complement we add 1, to get 2’s complement.

Therefore, 2’s complement of 25 is

2's complement

Converting 2's complement to integer value

Steps:

  1. Convert 2’s complement to 1’s complement by subtracting 1 from 2’s complement.
  2. Flip all the bits of 1’s complement to get back the number. (Don’t flip the sign bit)

Step 1: Converting to 1's complement

computing 1's complement

Step 2: Converting back to number

getting back from 1's complement

Convert the bit pattern after flipping the bits to decimal. Convert without considering the sign bit. 11001 in decimal is 25 and sign bit is 1. Therefore the number is -25.

Decimal to Octal

To convert decimal number to octal we repeatedly divide the number by 8, till we get the quotient as 0. The octal number is base – 8 system, we can use digits from 0 to 7.

Ex : N = 115, to convert 115, we divide 115 repeatedly by 8. Then we write the remainders in bottom-up manner.

Decimal to octal

Therefore,

 (115)10 = (163)

Octal to Decimal

Same as the converting binary to decimal, we write octal number in placeholder format and multiply with 8 powers.

octal to decimal

Therefore,

 (163)8 = 1 * 82 + 6 * 81 + 3 * 80


= 115

Decimal to Hexadecimal

To convert decimal number to hexa-decimal we repeatedly divide the number by 16, till we get the quotient as 0. The octal number is base – 16 system, we can use digits from 0 to 9 and A to F.

Ex : N = 62, to convert 62, we divide 62 repeatedly by 16. Then we write the remainders in bottom-up manner.

decimal to hexa-decimal

16 * 3 = 48, 14 is the remainder (14 in hex is E)

16 * 0 = 0, 3 is the remainder

 (62)10 = (3E)16

Hexa-decimal to Decimal

Same as the converting binary to decimal, we write hexa-decimal number in placeholder format and multiply with 16 powers.

hexa-decimal to decimal

 (3E)16 = 3*161 + 14 * 160 

Binary to Octal

In octal the maximum number that we can write is 7. 7 in binary takes three bits. Therefore we go on grouping three-three bits from RHS and write the equivalent of it.

Let us understand this with an example. 

Binary to octal

Octal to Binary

It is the reverse process of Binary to octal. We write three bits for each octal number.

Let us understand this with an example.

Let the octal number be, 165, we write three bits for each octal number.

Therefore 0165 in binary = 001 110 101

Binary to Hexa-decimal

In hexa-decimal the maximum number that we can write is 15. 15 in binary takes four bits. Therefore, we go on grouping four-four bits from RHS and write the equivalent of it.

Let us understand this with an example. 

binary to hexa decimal

Hexa-decimal to Binary

It is the reverse process of Binary to Hexa-decimal. We write four bits for each hexa-decimal number.

Let us understand this with an example.

Let the octal number be, 0X16A, we write three bits for each octal number.

Therefore 0x16A in binary = 0001 0110 1010

Scroll to Top