Number Systems – Binary, Hex, Decimal

Number Systems

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 – 12310 , Base 2 for binary – 1100102, Base 16 for hexadecimal – 1F416 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:

  1. Decimal number system (Base -10)
  2. Binary number system (Base -2)
  3. 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 × 102
  • In 159 , value of 5 is 5 tens or 50 or 5 × 10 or 5× 101
  • In 265, value 0f 5 is 5 units or 5 or 5 × 1 or 5 × 100

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 * 26
32 = 1 * 25
4 = 1 * 22
10010 = 1 * 26 + 1 * 25 + 0 * 24 + 0 * 23 + 1 * 22 + 0 * 21 + 0 * 20
10010 = 11001002

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

10010 = ?(hex)
100 = 6 * 16 + 4 = 6 * 161 + 4 * 160
10010 = 6416

Conversions between Number Systems

conversion table

Converting Binary to Decimal Number System

Steps to follow :

  1. Start from the last(right-most) digit. It has a “weight” of (2 to the power of 0) = 1.
  2. If that digit is 1, add 1 to the result.
  3. 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.
  4. Repeat step 3 until you pass through all positions.

Here is an example:

10112 = ? (dec)

Binary to decimal conversion

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

10112 = 1110

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 :

  1. 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.
  2. 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.
  3. Substitute each group with its corresponding hex value from the table below. The result is your hex number.

Hex to bin conversion table

Here is an example:

101002 = ?16

101002 = 1 01002
1 01002 = 0001 01002
00012 = 116, 01002 = 416
101002 = 1416

Lets take one more example –

1100011010100000002 = ?16
110001101010000000 = 11 0001 1010 1000 0000
11 0001 1010 1000 0000 = 0011 0001 1010 1000 0000
0011 0001 1010 1000 00002 = 31A8016

Converting Hexadecimal to Decimal Number System

Here are the steps to convert hex to decimal:

  1. Start from the right-most digit. Its weight(or coefficient) is 1.
  2. 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)
  3. Move one digit to the left. Its weight is 16 times the previous weight.
  4. 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.

3216 = 2 + 48 = 5010

One more example –

Example for converting hex to decimal number

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 20 (1), the next one equals to 21 (2), the next one equals to 22 (4) and the leftmost one equals to 23 (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

6416 = ?2
    616 = 01102, 416 = 01002
6416 = 011001002 or 0110 01002

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.

Sharing is Caring!!

Leave a Reply

Your email address will not be published. Required fields are marked *