NUMBER  SYSTEMS  for computers

Number System in C++


Decimal Number System


Decimal number system is a base 10 number system having 10 digits from 0 to 9. This means that any numerical quantity can be represented using these 10 digits. Decimal number system is also a positional value system. This means that the value of digits will depend on its position. Let us take an example to understand this.

Say we have three numbers – 734, 971 and 207. The value of 7 in all three numbers is different−

  • In 734, value of 7 is 7 hundred or 700 or 7 × 100 or 7 × 10

  • In 971, value of 7 is 7 tens or 70 or 7 × 10 or 7 × 101

  • In 207, value of 7 is 7 units or 7 or 7 × 1 or 7 × 100


The weightage of each position can be represented as follows −

 

In digital systems, instructions are given through electric signals; variation is done by varying the voltage of the signal. Having 10 different voltages to implement decimal number system in digital equipment is difficult. So, many number systems that are easier to implement digitally have been developed. Let’s look at them in detail.


Binary Number System


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, though 0 is not actually no signal but signal at a lower voltage. The number system having just these two digits – 0 and 1 – is called binary number system.

Each binary digit is also called a bit. Binary number system is also positional value system, where each digit has a value expressed in powers of 2, as displayed here.

In any binary number, the rightmost digit is called least significant bit (LSB) and leftmost digit is called most significant bit (MSB).



Octal Number System


Octal number system has eight digits – 0, 1, 2, 3, 4, 5, 6 and 7. Octal number system is also a positional value system with where each digit has its value expressed in powers of 8, as shown here

In Computer language, octal number is defined as a sequence of digits starting with 0(zero). For instance, decimal number 8 is represented as 010(since 810=0108).

Caution: When you start a number with 0, make sure you do not use digits 8 or 9 as the octal system doesn’t recognise digits other than 0 to 7.

 

Hexadecimal Number System

Hexadecimal number system has 16 symbols – 0 to 9 and A to F where A is equal to 10, B is equal to 11 and so on till F. Hexadecimal number system is also a positional value system with where each digit has its value expressed in powers of 16, as shown here



SYMBOL

MEANING

A

B

C

D

E

F

10

11

12

13

14

15

 

A hexadecimal number is a sequence of digits and alphabets starting with 0x or 0X. For instance, 12 in decimal is written as 0xC in hexadecimal.

Caution: When you start a number with 0x or 0X, make sure the alphabets range from A to F in capitals.

 

NUMBER   SYSYTEM   CONVERSIONS


We may enter an integer in the form of any number system. As discussed in the previous section, all number systems come with a predefined set of valid characters and digits. But to communicate to it to the computer is a tough job as computer understands nothing but electronic signals.

Let us understand this using a daily life example: Everyone is familiar with the time table used in schools, where the week is divided into say 5 working days and each of the days have a fixed schedule. Now imagine instead of days if we use dates of months, we would have 28,29,30 or 31 sets of schedules! Technically speaking the latter is more elaborate than the first but it is really tough to manage.

Similarly, octal, decimal or hexadecimal number systems are more elaborate; but they use a lot of digits or even characters(in case of hexadecimal). It would become a lengthy and hectic job to create electronics signals corresponding to each digit and character. It would be time consuming and prone to manufacturing and programming mistakes.  

For this exact reason Binary Number System is chosen as the base for any electronic or digital interaction. It has only 2 digits (0 and 1) and can represent any integer using these two. Therefore, it becomes important to learn how to convert numbers from other number systems to binary.


CONVERSION FROM DECIMAL TO BINARY


Q. Convert 3510 to binary.

The steps are:

Divide the given number by 2 and note the quotient and remainder. Obviously, the remainder must be either 0 or 1. Divide the quotient again by 2 and note the new quotient and remainder. Repeat this process till the quotient becomes either 0 or 1. Read the last quotient(either 0 or 1) and all the remainders from last till the first. It gives the equivalent binary code.


Step 1: Divide the number by 2 and note quotient and remainder:


35/2 >>>> Quotient = 17 Remainder = 1


Step 2: Divide the quotient by 2 and note the new quotient and remainder:


17/2>>>> Quotient = 8 Remainder = 1


Step 3: Repeat step 2 till the new quotient becomes 0 or 1:


8/2>>>>  Quotient = 4 Remainder = 0

4/2>>>> Quotient = 2  Remainder = 0

2/2>>>> Quotient = 1 Remainder = 0


Step 4: The binary equivalent will be the last quotient and all the remainders taken from last to first.

See the following image for explanation:


Therefore 3510 = (100011)2


CONVERSION FROM BINARY TO DECIMAL


Convert (100011)2 to Decimal System.


Write the binary code as written above, with corresponding powers of 2. The decimal number will be the sum of all the products of digits with the corresponding powers of 2.


Decimal Number= 1 X 25 + 0 X 24 + 0 X 23 + 0 X 22 + 1 X 21 + 1 X 20

                           = 32 + 0 + 0 + 0 + 2 + 1

                           = 35


CONVERSION FROM OCTAL TO BINARY

 

Conversion from octal to binary is rather easy. We just need to separate each digit of the octal number and replace it with the corresponding binary code. Each digit corresponds to a 3 bit binary code representing the digit in decimal form.

DIGIT

BINARY CODE

0

000

1

001

2

010

3

011

4

100

5

101

6

110

7

111

 

 

 

 

 

 

 

                                               

For Example, let us convert 052 to binary. (note that a number starting with 0 is octal).

Step 1: Separate the digits:

5

2

                      

 

Step 2: Replace the digits with corresponding binary code:


Step 3: The Binary form is the combination of the binary codes:

052 = (101010)2

Note: The 0(zero) on the left most position is immaterial and is just to represent an octal number.

 

CONVERSION FROM BINARY TO OCTAL

 

It is just the reverse of the above process (conversion from octal to binary). You need to divide the given binary number in group of 3’s from right to left and adding 0(zeroes) to the left if required. Then replace each 3 bit binary code to the corresponding digit according to the above table (i.e. replace the binary with its equivalent decimal number). The resulting number is the octal form of the given binary.

For Example, let us convert (11101010)2 to octal.

Step 1: Group given binary in group of 3 from right to left (add zeroes in left if needed):

011

101

010

                      

 

Step 2: Replace each group with the equivalent decimal number( Refer to the table above):


Step 3: Combine the digits and add 0 at starting position (to represent it is an octal number):

(11101010)2 = 0352

 

CONVERSION FROM HEXADECIMAL TO BINARY

 

Conversion from hexadecimal to binary is similar to conversion from octal to binary. The only difference is we need a four bit binary code to represent the digits of hexadecimal number. The following table gives the conversions:

DIGIT

BINARY CODE

0

0000

1

0001

2

0010

3

0011

4

0100

5

0101

6

0110

7

0111

8

1000

9

1001

A

1010

B

1011

C

1100

D

1101

E

1110

F

1111

To convert a hexadecimal number to binary, separate each digit of the given number and replace it with the equivalent 4 bit binary code. Then combine the binary codes to generate the binary equivalent of the given hexadecimal number.


For Example, let us convert 0xA25 to binary


Step 1: Separate the digits:

A

2

5

 

 

 Step 2: Replace the digits with the equivalent 4 bit binary code:



Step 3: Combine the binary codes:

0xA25 = (101000100101)2