Number System in C Programming
Number System in C Programming
Number System
Number System
2
Decimal Number System
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
9
Number System Conversions
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
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
5
= 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
25
Decimal ()10 to Binary ()2
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
29
Decimal ()10 to Binary ()2
31
Decimal ()10 to Binary ()2
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
So,=
35
Decimal ()10 to Octal ()8
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
=
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
45
Binary ()2 to Octal ()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
50
Binary to Hexadecimal
= (1010101101001) 2
= (1 5 6 9) 16
= (1569) 16
51
001100101.110111 into hexadecimal number
= (001100101.110111)
= (0 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
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
63
Example
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
65
Example
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
67
Example
68
Binary Number
Representation
• Unsigned Representation
• Signed Representation
69
Unsigned Representation
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)
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