We use decimal/base 10 number system in our day to day lives and it is made up of 10 digits – from 0 to 9. Every number in this number system consists of only these 10 digits and can be represented by powers of 10. For example, in the number 1234, 4 is at 1's place, 3 is at 10's place, 2 is at 100's place and 1 is at 1000's place and thus we can write 1234 as 1*10^{3} + 2*10^{2} + 3*10^{1} + 4*10^{0}.

## Binary Number System

As decimal (or base 10) number system consists of 10 digits (0 to 9), an arbitrary number system of base 'n' consist of n digits (0 to n-1). So, binary number system (or base 2) consists of 2 digits (0 and 1) and every number in this number system can be represented by these two digits only.

### Why binary number system is useful in computers?

Nowadays, digital system is used to store information that is stored using the difference in voltage levels. For example, a low (or 0) can be represented using 0 volts and a high (or 1) can be represented by 5 volts. The circuits in a computer's processor are made up of billions of transistors and each of these transistors can attain two states of on and off and we use 0 and 1 to reflect these on and off. Thus, we use binary number system in computers.

So, every computer program first gets converted to these binary codes (machine code) and then this code makes the computer do our work. A common question that arises here is that can we use three states (low, medium and high) instead of only two and use base 3 number system? In principle, we can use a larger base number and a larger set of voltages. In practice, greater the number of voltage states used, greater will be the difficulty (or error/noise) in designing the circuit to hold these states. For example, a two state "switch" can be either on or off but a three state "switch" can attain three states and let's say these three states are 0, 1 and 2 volts then where would we put 1.5 volts? Also, the voltage measured with different circuits will be a little different, and can be 1.4 or 1.6 volts in another system, putting the voltage in different states. Also, we can't use larger voltage difference because it will cause overheating of the system. This is similar to asking two persons to rate a movie. They both can say that the movie is either good or bad. But when we ask them to rate the movie on a scale of 1 to 10, then their ratings can be different.

So, two major reasons are:

- Binary devices are easy to make
- Binary system will have much less noise

However, quantum computers are in research which can hold multiple states. You can read more about them on Wikipedia.

## Conversion of decimals to binary

To convert a number from decimal to binary, we just divide the number by 2 and note down the remainder and then again divide the quotient by 2. We repeat this process until the quotient becomes 0. For example, let's convert 8 into binary.

Number | Quotient (div by 2) | Remainder |
---|---|---|

8 | 4 | 0 |

4 | 2 | 0 |

2 | 1 | 0 |

1 | 0 | 1 |

So, the binary equivalent of 8 is 1000.

Let's convert 123 to binary.

Number | Quotient (div by 2) | Remainder |
---|---|---|

123 | 61 | 1 |

61 | 30 | 1 |

30 | 15 | 0 |

15 | 7 | 1 |

7 | 3 | 1 |

3 | 1 | 1 |

1 | 0 | 1 |

So, the binary equivalent of 123 is 1111011.

### C code to convert decimals to binary

```
#include <stdio.h>
int main()
{
//decimal to binary convertor
int num,rem;
printf("Enter the number\n");
scanf("%d",&num);
long binary = 0, i=1;
while(num>0)
{
rem = num%2;
num = num/2;
binary = binary + (rem*i);
i = i*10;
}
printf("%li\n",binary);
return 0;
}
```

## Conversion of binary to decimal

As we use powers of 10 in decimal, we can use powers of 2 to convert a binary number into decimal. For example, 101 in decimal is 1*2^{2}+0*2^{1}+1*2^{0} i.e., 5.^{ }

### C code to convert binary to decimal

```
#include <stdio.h>
int main()
{
//decimal to binary convertor
int num = 0,rem;
long binary;
printf("Enter the binary number\n");
scanf("%li",&binary);
int pow_of_two = 1;
while(binary>0)
{
//taking the last digit of the binary
rem = binary%10;
//making decimal value
//we have to multiply with powers of 2
num = num + (rem*pow_of_two);
binary = binary/10;
//increasing power of 2
pow_of_two = pow_of_two*2;
}
printf("%d\n",num);
return 0;
}
```

## Hexadecimal Number System

Hexadecimal number system is base 16 number system. As we know that a base n number system contains n digits from 0 to n-1 so, base 16 number system should contain 16 digits. It contains digits from 0 to 9 and six letters A, B, C, D, E and F to represent the numbers 11, 12, 13, 14, 15 and 16 respectively.

### Conversion of hexadecimal to decimal

Similar to binary, we use powers of 16 to convert a hexadecimal to decimal. For example, 7DE in decimal system is (7 * 16^{2}) + (13 * 16^{1}) + (14 * 16^{0}) i.e., 2014.

### Conversion of decimal to hexadecimal

Similar to binary, we just do the division by 16 instead of 2 to convert a decimal to hexadecimal.