0% found this document useful (0 votes)
354 views

Number System in C Programming

The document provides information about different number systems including decimal, binary, octal, and hexadecimal number systems. It explains the base and digits used in each system and how to convert between decimal, binary, octal, and hexadecimal numbers. The key details covered are the bases (10, 2, 8, 16), digits (0-9, 0-1, 0-7, 0-9 & A-F), and methods for converting between number systems by multiplying place values by the base.

Uploaded by

Kashish Gulati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
354 views

Number System in C Programming

The document provides information about different number systems including decimal, binary, octal, and hexadecimal number systems. It explains the base and digits used in each system and how to convert between decimal, binary, octal, and hexadecimal numbers. The key details covered are the bases (10, 2, 8, 16), digits (0-9, 0-1, 0-7, 0-9 & A-F), and methods for converting between number systems by multiplying place values by the base.

Uploaded by

Kashish Gulati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

Amity School of Engineering and Technology

Number System
Number System

• Decimal Number System


• Binary Number System
• Octal Number System
• HexaDecimal Number System

2
Decimal Number System

• 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.
• In decimal number system, the successive
positions to the left of the decimal point
represents units, tens, hundreds,
thousands and so on.

3
Decimal Number System
• Each position represents a specific power
of the base (10).
• For example, the decimal number 1234
consists of the digit 4 in the unit's position,
3 in the tens position, 2 in the hundreds
position, and 1 in the thousands position,
and its value can be written as
=((4 × 1) + (3 × 10) + (2 × 100) + (1 × 1000))
=1234
4
Binary Number System
• Uses two digits, 0 and 1.
• Also called base 2 number system
• Each position in a binary number
represents a 0 power of the base (2).
• Example: 20
• Last position in a binary number
represents an x power of the base (2).
• Example: 2x where x represents the last
position - 1.
5
Octal Number System
• Uses eight digits, 0,1,2,3,4,5,6,7.
• Also called base 8 number system
• Each position in an octal number
represents a 0 power of the base (8).
Example: 80
• Last position in an octal number
represents an x power of the base (8).
• Example: 8x where x represents the last
position - 1.
6
Hexadecimal Number System
• Uses 10 digits and 6 letters,
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
• Letters represents numbers starting from 10. A =
10, B = 11, C = 12, D = 13, E = 14, F = 15.
• Also called base 16 number system.
• Each position in a hexadecimal number
represents a 0 power of the base (16). Example
160.
• Last position in a hexadecimal number
represents an x power of the base (16). Example
16x where x represents the last position - 1.
7
Values of Each Digit
• A value of each digit in a number can be determined
using
• The digit
• The position of the digit in the number
• The base of the number system (where base is defined
as the total number of digits available in the number
system).
Number System
S.No. Number System Base Digits
1 Decimal number system 10 0-9
2 Binary Number System 2 0-1

3 Octal Number System 8 0-7

4 Hexa Decimal Number 16 0-9 & A-F


System Numeric
Equivalent
A-10
B-11
C-12
D-13
E-14
F-15

9
Number System Conversions

• Binary ()2 to Decimal ()10


• Octal ()8 to Decimal ()10
• Hexa Decimal ()16 to Decimal ()10

10
Binary ()2 to Decimal ()10
• In the binary number
system, the weight of
each digit increases
by a factor of  2
• First digit has a
weight of  1 ( 20 ), the
second digit has a
weight of  2 ( 21 and
so on.

11
Binary ()2 to Decimal ()10
• (10101)2 to ()10
Step 1: Multiply each digit by base from LSB and take power starting

from 0

=((1 × 20) + (0 × 21) + (1 × 22) + (0 × 23) + (1


× 24))10
Step 2: Perform the mathematical calculation

=((1 × 1) + (0) + (1 × 4) + (0) + (1 × 16)) 10


=((1) + (0) + (4) + (0) + (16))10
=(21)10 12
Binary ()2 to Decimal ()10
• Fractional digits to the
right of the binary
point have respective
weightings
• The weightings are
negative powers of
two, creating a binary
fraction i.e. 2-1, 2-2, 2-3,
2-4

13
Binary ()2 to Decimal ()10

• (1101.0111)2 to ()10
1101.0111 = (1×23) + (1×22) + (0×21) + (1×20)
 + (0×2-1) + (1×2-2) + (1×2-3) + (1×2-4)
 = 8 + 4 + 0  + 1 + 0 + 1/4 + 1/8  + 1/16
 = 8 + 4 + 0  + 1 + 0 + 0.25 + 0.125  + 0.062

= 13.437510

14
Binary ()2 to Decimal ()10

• (1011.111)2 to ()10
1011.111 = (1×23) + (1×21) + (1×20) +(1×2-
1
) + (1×2-2) + (1×2-3)
= 8 + 2 + 1 + 0.5 + 0.25 + 0.125 = 11.87510

• 11.001 = (1×21) + (1×20) + (1×2-
3
) = 2 + 1 + 0.125 = 3.12510

15
Octal ()8 to Decimal ()10
• Base of an Octal
Numbers system
is 8 (base-8)
• octal number system
uses only eight digits
(0 through 7)

16
Octal ()8 to Decimal ()10

• (232)8 to ()10
 =( 2×82 ) + ( 3×81 ) +  ( 2×80)
=2x64 + 3x8 + 2x1
=128+24+2 =154
• (1576)8 to ()10
=1x8 3
 + 5x8 2
 + 7x8 1
 + 6x80

=
1x512 + 5x64 + 7x8 + 6x1
=512 + 320 + 56 + 6 = 894
17
Octal ()8 to Decimal ()10

18
Octal ()8 to Decimal ()10

• (7.12172)8 to (?)10
= (7×80)+ (1×8-1) + (2×8-2) + (1×8-3)+  (7×8-
4
) + (2×8-5)
=7+0.125+0.03125+0.001953125+0.001708
984375+0.00006103515624
=10.1599..
=10.16(approx.)

19
Octal ()8 to Decimal ()10

• (152.54)8 to (?)10
= (1 × 8²) + (5 × 8¹) + (2 × 8⁰) + (5 × 8⁻¹) +
(4 × 8⁻²)
=1 ×64 +5 ×8+2 ×1+5 ×0.125+4 ×0.015625
=106.6875

20
Hexa Decimal ()16 to
Decimal ()10
• (AB34)₁₆ = (?)₁₀
= (10 × 16³) + (11 × 16²) + (3 × 16¹) + (4 ×
16⁰)
=(10 × 4096) + (11 × 256) + (3 × 16) + (4 ×
1)
=40960+2816+48+4
= (43828)₁₀

21
Hexa Decimal ()16 to
Decimal ()10
• (AB34)₁₆ = (?)₁₀
= (10 × 16³) + (11 × 16²) + (3 × 16¹) + (4 ×
16⁰)
=(10 × 4096) + (11 × 256) + (3 × 16) + (4 ×
1)
=40960+2816+48+4
= (43828)₁₀

22
Hexa Decimal ()16 to
Decimal ()10
( EF.B1)16= ( ? )10
=E x 161 + F x 160 +  B x 16-1 + 1 x 16-2  
= 14 x 16  + 15 x 1 +  11 x ( 1 / 16 ) + 1 x
( 1 / 256 )  
= 224   + 15 . ( 0. 6 8 7 5 ) +  ( 0 . 0 0 3 9 0 6
2 5 ) 
= 239  +  0. 6914
= 239 . 691406
23
Test Your Understanding
• (10111.1101)2 to ()10
• (247.35)8 to ()10
• (A214.9E)16 to ()10

24
Number System Conversions

• Decimal ()10 to Binary ()2


• Decimal ()10 to Octal ()8
• Decimal ()10 to Hexa Decimal ()16

25
Decimal ()10 to Binary ()2

• Write down the decimal number and


continually divide-by-2 (two) to give a
result and a remainder of either a “1” or a
“0” until the final result equals zero.

26
Decimal ()10 to Binary ()2

• Convert =

• =(

27
Decimal ()10 to Binary ()2

• =
Step-1 Begin with the decimal fraction and
multiply by 2. The whole number part of the
result is the first binary digit to the right of
the point.
.625 x 2 = 1.25, the first binary digit to the
right of the point is a 1. So far, we have .625
= .1??? . 

28
Decimal ()10 to Binary ()2

 Step-2 Discard the whole number part of


the previous result (1 in this case) and
multiply by 2 once again
.25 x 2 = 0.50
So,.625 = .10??

29
Decimal ()10 to Binary ()2

Step -3 Discard the whole number part of


the previous result (.50 in this case) and
multiply by 2 once again
.50 x 2 = 1.00
So,.625 = .101?
We are finished in Step 3, because we had
0 as the fractional part of our result there.
So, =
30
Decimal ()10 to Binary ()2

Infinite Binary Fractions


Covert =
Step1. .1 x 2 = 0.2
Step 2. .2 x 2 = 0.4
Step 3. .4 x 2 = 0.8
Step 4. .8 x 2 = 1.6
Step 5. .6 x 2 = 1.2

31
Decimal ()10 to Binary ()2

Infinite Binary Fractions


Step 6. We multiply by 2 once again,
discarding the whole number part of the
previous result. Notice that this next step to
be performed (multiply .2 x 2) is exactly the
same action we had in step 2.
In other words, we will never get a 0 as the
decimal fraction part of our result. Instead
we will just cycle through steps 2-5 forever. 
32
Decimal ()10 to Binary ()2

Infinite Binary Fractions


 So, =

33
Decimal ()10 to Octal ()8

=
Step 1 Divide 567 by 8
Step 2 Continue dividing the quotient by 8
until you get a quotient of zero
Step 3 Finally write the remainders in the
reverse order to get octal equivalent of
decimal number 567

34
Decimal ()10 to Octal ()8

Hence, by continuously dividing the decimal


number by 8 we get

So,=
35
Decimal ()10 to Octal ()8

• To convert a decimal fraction to octal,


multiply by 8;
• the integer part of the result is the first digit
of the octal fraction.
• Repeat the process with the fractional part
of the result, until it is null or within
acceptable error bounds.

36
Decimal ()10 to Octal ()8

• =
×8=1.3125
0.3125×8=2.5
0.5×8=4.0
So, =

37
Decimal ()10 to Octal ()8

• =
=
0.16×8=1.28
0.28×8=2.24
0.24×8=1.92
0.92×8=7.36
0.36×8=2.88
So, =
38
Decimal ()10 to Hexa
Decimal ()16
• First, divide the decimal number by 16,
• Keep aside the remainder left.
• Again divide the quotient by 16 and repeat
till you get the quotient value equal to
zero.
• Now take the values of the remainder’s in
the reverse order to get the hexadecimal
numbers.

39
Decimal ()10 to Hexa
Decimal ()16
=

 Read from the bottom (MSB) to top (LSB)


as 3C0
=

40
Decimal ()10 to Hexa
Decimal ()16
• To convert a decimal fraction to
hexadecimal, multiply by 16;
• The integer part of the result is the first
digit of the hexadecimal fraction.
• Repeat the process with the fractional part
of the result, until it is null or within
acceptable error bounds.

41
Decimal ()10 to Hexa
Decimal ()16
=
1. 0.06640625 x 16=1.0625
2. 0.0625 x 16 =1.0
3. 0 x 16=0.0

42
Test Your Understanding

• (296.5)10 to ()2
• (813.24)10 to ()8
• (479.36)10 to ()16

43
Octal ()8 to Binary ()2

44
Binary ()2 to Octal ()8

• Starting from the LSB, make groups of


three bits.
• If there are one or two bits less in making
the groups, 0s can be added after the
MSB
• Convert each group into its equivalent
octal number

45
Binary ()2 to Octal ()8

So, (010110010101)2 =(2625)8

46
Octal ()8 to Binary ()2

• (145)8=(?)2
=(001 100 101)8
=(1100101)8
• (352.563)8=(?)2
=(011 101 010.101 110 011)2
=(011101010.101110011)2

47
Test your Understanding

• (234.56)8=(?)2
• (124.05)8=(?)2

48
Hexadecimal Number
Hexadecimal Binary
Number Number
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
49
Binary to Hexadecimal

• Starting from the LSB make groups of four


bits.
• If there are one or two bits less in making
the groups, 0s can be added after MSB.
• Convert each group into its equivalent
octal number.

50
Binary to Hexadecimal

= (1010101101001) 2

= (1 0101 0110 1001) 2

= (0001 0101 0110 1001) 2

= (1 5 6 9) 16

= (1569) 16

51
001100101.110111 into hexadecimal number

= (001100101.110111)
= (0 0110 0101 . 1101 1100)
2

= (0110 0101 . 1101 1100)


2

= (6 5 . D C) = (65.DC)
2 16 16
52
Hexa decimal()16 to Binary ()2

• (2C1)16 = (001011000001)2
• (9DB2)16 = (1001110110110010)2

53
Test your Understanding

• (234.56)16=(?)2
• (124.05)16=(?)2

54
Binary Arithmetic

• The arithmetic of binary numbers means
the operation of binary
addition, binary subtraction, binary multi
plication and binary division.

55
Binary Addition

• There are four steps in binary addition


they are written below
• 0+0=0
• 0+1=1
• 1+0=1
• 1 + 1 = 0 (carry 1 to the next significant bit)

56
Example

57
Example

• 10111 + 110001=?
• 1011001 + 111010=?

58
Binary Subtraction

• Rules
0-0=0
1-0=1
1-1=0
0 - 1 = 1 with a borrow of 1

59
60
Example

• 1110110-1010111

61
1110110-1010111

62
Example

• 100010110 − 1111010 = 10011100:

63
Example

• Subtract 11010.101 from 101100.011

        1 0 1 1 0 0 . 0 1 1

           1 1 0 1 0 . 1 0 1

           1 0 0 0 1 . 1 1 0

64
Binary Multiplication

• The rules of binary multiplication are:


0×0=0
0×1=0
1×0=0
1 × 1 = 1 [No borrow or carry method is
applicable here]

65
Example

• 9×5 (1001 × 101)

1 0 0 1
× 1 0 1
1 0 0 1
0 0 0 0
1 0 0 1
1 0 1 1 0 1

66
Binary Division

• The main rules of the binary division


include:
• 1÷1 = 1
• 1÷0 = 0
• 0÷1 = Meaningless
• 0÷0 = Meaningless

67
Example

68
Binary Number
Representation
• Unsigned Representation
• Signed Representation

69
Unsigned Representation

• Non negative numbers can be represented


• No>=0
• Negative numbers cannot be represented

Signed representation
• All integers can be represented
• Nos can be positive, negative or zero

70
Ways to Represent
Signed Numbers
• Sign and magnitude (SNM)
• 1’s complement
• 2’s complement

71
72
Example
Decimal Unsigned Sign and 1’s 2’s
Number Magnitude complement complement(
1’s
complement
+1)
+10 1010 0 1010 0 1010 0 1010
-10 … 1 1010 1 0101 1 0110
(shown in
table)
- 25 (6-bit 1 11001 1 00110 1 00111
required)
+ 25 11001 0 11001 0 11001 0 11001

73
Range of Example
From To
Unsigned (total 8 0 7
representation)
Sign and Magnitude -3 +3
(total 7 distinct value, 0
repeated)
1’s complement (total 7 -3 +3
distinct value, 0
repeated)

2’s complement (all 8 -4 +3


representation)

74
8-bit Number

• =256
Unsigned-0 to 255
Signed and Magnitude- -127 to +127
1’s complement- -127 to +127
2’s complement- -128 to +127
• =1024
Unsigned-0 to 1023
Signed and Magnitude- -511 to +511
1’s complement- -511 to +511
2’s complement- -512 to 511
75
More Examples
Decimal Unsigned Sign and 1’s 2’s
Number Magnitude complement complement(
1’s
complement
+1)
+ 50 110010 0 110010 0 110010 0 110010
- 50 Cannot be 1 110010 1 001101 1 001110
represented
8 1000 0 1000 0 1000 0 1000
-8 1 1000 1 0111 1 1100
+31 11111 0 11111 0 11111 0 11111
-31 1 11111 1 00000 1 00001

76
References
 ANSI 89 – American National Standards Institute, American
National Standard for Information Systems Programming
Language C, 1989.
 Kernighan 78 – B. W. Kernighan and D. M. Ritchie, The C
Programming Language, Prentice-Hall: Englewood Cliffs, NJ,
1978. Second edition, 1988.
 Thinking 90 – C* Programming Guide, Thinking Machines
Corp. Cambridge Mass., 1990.
 Programming in ANSI C, E. Balagurusamy. McGrawHill
 Let Us C, Yashvant Kanetkar, BPB Publications
 Programming in C, Reema Thareja, Oxford University Press

77
Thank You

You might also like