2015-02-02

Number Systems in C

===================

There are 4 types of Number systems in the C language

1. Decimal

2. HexaDecimal

3. Binary

4. Octal

Among them Ocatl foramt is rarely used.

Decimal

=======

It uses numbers from 0 to 9. Its base is 10

Ex: 123 and 4568 90000

Hexadecimal

===========

It uses numbers from 0to 9 and also Ato F .

A represents 10 , B represents 11 and so on upto F(15)

Its base is 16.

0x can be used a prefix to indicate the number as a hexadecimal

Ex: 0xFB1 0x7BC

Binary

=====

It uses numbers 0 and 1. Its base is 2

Ex: 01010 , 10100

Octal

======

It uses numbers 0 and 7. Its base is 8

0 can be used a prefix to indicate the number as a octal

Ex: o784 0898

This system is rarely used dont worry about that

Conversions from one system to Another:

=======================================

1. Any number system to Decimal:

=================================

To convert to decimal u need to sum all the exponentials of the systems bases

Ex: abcd and base is n

Then

a*n^3+b*n^2+c*n^1+ d*n^0 ----->(1)

a. HexaDecimal to Decimal

=========================

Ex: 0xF12

As it Hexadecimal conatins base 16 so from (1)

= F*16^2+1*16^1+2*16^0

= 15*256+1*16+2*1 (as F is 15)

= 3858

b.Binary to Decimal

=========================

Ex: 10101

As it Binary conatins base 2 so from (1)

= 1*2^4+0*2^3+1*2^2+0*2^1+1*2^0

= 1*16+0*8+1*4+0*2+1*1

= 21

2. Decimal to Other Formats

=======================

To convert to other formats u need to divide the decimal number by the systems base and has to accumalte remainders.

a. Decimal to Hexadecimal

==========================

U need to divide by 16 as Hexadecimal base is 16 .

Ex: 456

16 |456

--------------

16 |28 - 8(remainder)

-------------

| 1 - C(12)(reaminder)

--------------

so the number is 1C8

a. Decimal to Binary

==========================

U need to divide by 2 as Binary base is 2 .

Ex: 31

2|31

--------

2| 15 - 1

---------

2| 7 - 1

-------

2| 3 -1

----------

| 1 -1

so the number in binary is 11111

Excersises:

===========

1. Convert 0xAB12 , 0xc4f to Decimal

2. Convert 101011, 1011001 t0 Decimal

3. Convert 678 , 246 , 346 to Hexadecimal and also Binary

Show more