The number systems give us different ways to represent numbers. It defines a set of values to represent a quantity. The systems that are interesting for us, in programming, are the **decimal(dec), binary(bin) and hexadecimal(hex)**.

## Base

A **base** of a number system (or notation) is the number of symbols that we use to represent the numbers. When a number is written, its base should be written after the number in subscript.

**Example**– Base 10 for decimal – 123_{10 }, Base 2 for binary – 110010_{2}, Base 16 for hexadecimal – 1F4_{16} etc

**If the base is not provided**, the default is that the number is in decimal.

We`ll go through it again while explaining the types of number systems.

## Bits & Bytes

Digits 0 and 1 are called **bits** and 8 bits together make a **byte**. The data in computers is stored in terms of bits and bytes.

Computer memory is measured in terms of how many bits it can store. Here is a chart for memory capacity conversion.

- 1 byte (B) = 8 bits
- 1 Kilobytes (KB) = 1024 bytes
- 1 Megabyte (MB) = 1024 KB
- 1 Gigabyte (GB) = 1024 MB
- 1 Terabyte (TB) = 1024 GB

## Types of Number Systems

There are mainly 3 types of number systems in our programming world. They are as follows:

- Decimal number system (Base -10)
- Binary number system (Base -2)
- Hexadecimal number system (Base-16)

### Decimal Number Systems

The number system that we use in our day-to-day life is the decimal number system. Decimal number system has **base 10** as it uses 10 digits from **0 to 9**. It is also a **positional value system**. This means that the value of digits will **depend on its position**. Each position represents a specific **power of the base (10)**.

**Example **–

Say we have three numbers – 564, 159 and 265. The value of **5** in all three numbers is different−

- In 564, value of 5 is 5 hundreds or 500 or 5 × 100 or
**5 × 10**^{2} - In 159 , value of 5 is 5 tens or 50 or 5 × 10 or
**5× 10**^{1} - In 265, value 0f 5 is 5 units or 5 or 5 × 1 or
**5 × 10**^{0}

Later in this post we will see, how can we can convert other number systems to Decimal and vice versa.

### Binary Number Systems

The number system having just the two digits – 0 and 1 – is called **binary number system**. This is the natural “language” for our computers. Because the easiest way to vary instructions through electric signals is two-state system – on and off. On is represented as 1 and off as 0.

Binary Number system is also positional value system, where each digit has a **value expressed in powers of 2**.

#### Representation

To represent a number in binary you need to follow the universal rule: break down the number to **a sum of exact powers of 2.**

**Example –**

^{10010}`= ?(bin)`

`100 = 64 + 32 + 4`

`64 = 1 * 2`

^{6}

`32 = 1 * 2`

^{5}

`4 = 1 * 2`

^{2}

`100`

_{10}` = `

`1`

` * 2`

^{6}` + `

`1`

` * 2`

^{5}` + `

`0`

` * 2`

^{4}` + `

`0`

` * 2`

^{3}` + `

`1`

` * 2`

^{2}` + `

`0`

` * 2`

^{1}` + `

`0`

` * 2`

^{0}

`100`

_{10}` = 1100100`

_{2}

### Hexadecimal Number Systems

The hexadecimal number system uses 16 digits. It includes all decimal digits from 0 to 9 and adds the first 6 letters from the English alphabet. So all symbols are: **0 to and 9 and A to F** where A(hex) = 10(dec), B = 11, C = 12, D = 13, E = 14 and F = 15. There is no rule if the letters A to F are uppercase or lowercase.

#### Representation

Here, each digit has its **value expressed in powers of 16.**

**Example**–

`100`

_{10}` = ?(hex)`

`100 = 6 * 16 + 4 = `

`6`

` * 16`

^{1}` + `

`4`

` * 16`

^{0}

`100`

_{10}` = 64`

_{16}

## Conversions between Number Systems

### Converting Binary to Decimal Number System

**Steps to follow** :

- Start from the last(right-most) digit. It has a “weight” of (2 to the power of 0) = 1.
- If that digit is 1, add 1 to the result.
- Go one digit to the left. It has twice the weight of the previous digit. If that digit is 1, add its weight to the result.
- Repeat step 3 until you pass through all positions.

**Here is an example:**

1011_{2} = ? (dec)

Step 1: The right-most digit has a weight of 1.

Step 2: Its value is 1, so we begin to sum the result: result = 1

Step 3: The next digit to the left has twice the previous weight 1 * 2= 2. The digit in that position is 1, so add 2 to the result, result = 1+2.

Repeat 3: This next position has a weight of 2 * 2 = 4. The current digit is 0, so we don’t sum it and skip to the next digit to the left.

Repeat 3: The left-most position has a weight of 4 * 2 = 8. Its digit is 1, so we add 2 to the result, result = 1+2+8. since that was the last digit, we are done.

Result = 1 + 2+8 = 11

`1011`

_{2}` = 11`

_{10}

Try to convert several binary numbers with 2 to 5 digits for better understanding.

### Converting Binary to Hexadecimal Number System

Converting from binary to hex is a straightforward procedure.

**Steps to follow** :

- Take the binary number and mark every four digits as a separate group, starting from the right-most position. Do the grouping in a way, that is convenient to you – for instance, put a little extra space between them.
- The left most group could be incomplete ( size 1-2-3 digits, instead of four). If that is the case, add zeros to its left.
- Substitute each group with its corresponding hex value from the table below. The result is your hex number.

**Here is an example:**

`10100`

_{2}` = ?`

_{16}

`10100`

_{2}` = 1 0100`

_{2}

`1 0100`

_{2}` = 0001 0100`

_{2}

`0001`

_{2}` = 1`

_{16}`, 0100`

_{2}` = 4`

_{16}

`10100`

_{2}` = 14`

_{16}

Lets take one more example –

`110001101010000000`

_{2}` = ?`

_{16}

`110001101010000000 = 11 0001 1010 1000 0000`

`11 0001 1010 1000 0000 = 0011 0001 1010 1000 0000`

`0011 0001 1010 1000 0000`

_{2}` = 31A80`

_{16}

### Converting Hexadecimal to Decimal Number System

Here are the steps to convert hex to decimal:

- Start from the right-most digit. Its weight(or coefficient) is 1.
- Multiply the weight of the position by its digit. Add the product to the result.

*(0=0, 1=1, 2=2, … 9=9, A=10, B=11, C=12, D=13, E=14,F=15)* - Move one digit to the left. Its weight is 16 times the previous weight.
- Repeat 2 and 3 until you go through all hexadecimal digits.

**Example: (32) _{16} = ? (dec)**

Step 1: The coefficient of the right-most digit is 1.

Step 2: Multiply the coefficient(1) by the value of the digit(2): 1*2 = 2

Step 3: The weight of the next digit is 1 * 16 = 16

Repeat 2: Multiply the coefficient(16) by the value of the digit(3): 16 * 3 = 48

Repeat 3: No more positions to calculate.

The result is the sum of the results.

**32 _{16} = 2 + 48 = 50_{10}**

One more example –

### Converting Hexadecimal to Binary Number System

You just need to remember that each hex value will produce four binary digits.

**Step 1**: Write down the hex number. If there are any, change the hex values represented by letters to their decimal equivalents.**Step 2**: Each hex digit represents four binary digits and therefore is equal to a power of 2. The rightmost digit equals to 2^{0}(1), the next one equals to 2^{1}(2), the next one equals to 2^{2}(4) and the leftmost one equals to 2^{3}(8). Write these numbers (8, 4, 2 and 1) below the hex values.**Step 3**: Determine which powers of two (8, 4, 2 or 1) sum up to your hex digits. For example, if one of your hex values is 10, this means 8 and 2 sum up to 10 (4 and 1 are not used). If your hex number is 2, only 2 is used; 8, 4 and 1 are not.**Step 4**: Write down 1 below those 8, 4, 2 and 1’s that are used. Write down 0 below those that are not used.**Step 5**: Read the 1’s and 0’s from left to right to get the binary equivalent of the given hex number.

Let’s apply these steps to the hex number **(64) _{16}**

`64`

_{16}` = ?`

_{2}

` 6`

_{16}` = 0110`

_{2}`, 4`

_{16}` = 0100`

_{2}

`64`

_{16}` = 01100100`

_{2}` or 0110 0100`

_{2}

Lets take one more example –

`(4FA)`

_{16}` = ?(bin)`

`Step 1:`

`4 F A`

`4 15 10`

`Step 2:`

`4 F A`

`4 15 10`

`0100 1111 1010`

`Step 3:`

`(4FA)`

_{16}` = (10011111010)`

_{2}

## Video Tutorials on Number Systems:

The following videos will definitely help you in getting a better understanding on Number Systems:

Try playing with these conversions and you`ll understand better.