Number System Number System
Number System Number System
Number System Number System
Number System
Number System
Dr. A. Sahu
Dept of Comp. Sc. & Engg.
Dept of Comp. Sc. & Engg.
Indian Institute of Technology Guwahati
1
Outline
• Number System
– Decimal, Binary, Octal, Hex
D i l Bi O t l H
• Conversion (one to another)
– Decimal to Binary, Octal, Hex & Vice Versa
Decimal to Binary Octal Hex & Vice Versa
– Binary to HEX & vice versa
• Other representation
– Signed, Unsigned, Complement
• Operation
– Add, Sub, Mul, Div, Mod
• How to handle real number efficiently ?
– Float, Double
Float Double
2
What Digit? =>> Number System
What Digit? Number System
• Famous Number System: Dec, Rom, Bin
Famous Number System: Dec Rom Bin
• Decimal System: 0 ‐9
– May evolves: because human have 10 finger
M l b h h 10 fi
• Roman System
– May evolves to make easy to look and feel
– Pre/Post Concept: (IV, V & VI) is (5‐1, 5 & 5+1)
• Binary System, Others (Oct, Hex)
– One can cut an apple in to two
pp
3
Significant Digits
Significant Digits
Binary: 11101101
Binary: 11101101
Decimal:
Decimal: 1063079
• Two digits: 0, 1
• To make the binary numbers more
y
readable, the digits are often put in groups
of 4
– 1010 = 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20
= 8 + 2
= 10
= 10
– 1100 1001 = 1 * 27 + 1 * 26 + 1 * 23 + 1 * 20
= 128
128 + 64
64 + 8
8 + 1
1
= 201
How to Encode Numbers: Binary
Numbers
• Working with binary numbers
– In base ten, helps to know powers of 10
In base ten helps to know powers of 10
• one, ten, hundred, thousand, ten thousand, ...
– In base two, helps to know powers of 2
I b t h l t k f2
• one, two, four, eight, sixteen, thirty two, sixty
f
four, one hundred twenty eight
h d dt t i ht
• Count up by powers of two
29 28 27 26 25 24 23 22 21 20 a
Counting 0
1
00000
00001
0
1
0
1
2 00010 2 2
3 00011 3 3
4 00100 4 4
5 00101 5 5
6 00110 6 6
7 00111 7 7
8 01000 10 8
9 01001 11 9
10 01010 12 A
11 01011 13 B
12 01100 14 C
13 01101 15 D
14 01110 16 E
15 01111 17 F
10
16 10000 20 10
Fractional Number
Fractional Number
• Point: Decimal Point, Binary Point, Hexadecimal
point
• Decimal
247.75 = 2x102+4x101+7x100+7x10‐1+5x10‐2
• Binary
10.101= 1x21+0x20+1x2‐1+0x2‐2+1x2‐3
• Hexadecimal
6A.7D=6x161+10x160+7x16‐1+Dx16‐2
Converting To and From Decimal
Converting To and From Decimal
Successive
Decimal10
0 1 2 3 4 5 6 7 8 9 Weighted
Division
Weighted Successive Multiplication
M lti li ti
Multiplication Division
Successive Weighted
Di i i
Division M lti li ti
Multiplication
Octal8 Hexadecimal16
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 A B C D E F
Binary2
0 1
12
Decimal ↔ Binary
Decimal ↔ Binary
Successive
Division
a)) Divide the decimal number by 2; the remainder is the LSB of the binary
y ; y
number.
b) If the quotation is zero, the conversion is complete. Otherwise repeat step
(a) using the quotation as the decimal number. The new remainder is the
( ) g q
next most significant bit of the binary number.
Weighted
Multiplication
a) Multiply each bit of the binary number by its corresponding bit‐
weighting factor (i.e., Bit‐0→20=1; Bit‐1→21=2; Bit‐2→22=4; etc).
b) Sum up all of the products in step (a) to get the decimal number.
Decimal to Binary : Subtraction Method
y
• Goal Desired decimal number: 12
Desired decimal number: 12
– Good for human
32 16 8 4 2 1
– Get the binary weights to =32
1
add up to the decimal
dd t th d i l 32 16 8 4 2 1 too much
quantity 0 1 =16
• Work from left to right 32 16 8 4 2 1 too much
a
• (Right to left – may fill in 1s =8
0 0 1
that shouldn’t have been 32 16 8 4 2 1 ok, keep going
there – try it).
0 0 1 1 =8+4=12
32 16 8 4 2 1 DONE
0 0 1 1 0 0 answer
32 16 8 4 2 1
Decimal to Binary : Division Method
y
• Good for computer: Divide decimal number by 2 and
insert remainder into new binary number.
y
– Continue dividing quotient by 2 until the quotient is 0.
• Example: Convert decimal number 12 to binary
12 div 2 = ( Quo=6 , Rem=0) LSB
6 div 2 = (Quo=3, Rem=0)
( )
3 div 2 = (Quo=1,Rem=1)
1 div 2 = ( Quo=0 Rem=1) MSB
1 div 2 = ( Quo=0, Rem=1) MSB
1210= 1 1 00
1 1 00 2 2
Conversion Process Decimal ↔ BaseN
Successive
Division
a)) Divide the decimal number by N; the remainder is the LSB of the ANY
y ;
BASE Number .
b) If the quotient is zero, the conversion is complete. Otherwise repeat
step (a) using the quotient as the decimal number. The new remainder
p( ) g q
is the next most significant bit of the ANY BASE number.
Weighted
Multiplication
a) Multiply each bit of the ANY BASE number by its corresponding bit‐
weighting factor (i.e., Bit‐0→N0; Bit‐1→N1; Bit‐2→N2; etc).
b) Sum up all of the products in step (a) to get the decimal number.
Decimal ↔ Octal Conversion
Decimal ↔ Octal Conversion
The Process: Successive Division
• Divide number by 8; R is the LSB of the octal number
• While Q is 0
• Using the Q as the decimal number.
• New remainder is MSB of the octal number.
11
8 94 r = 6 ← LSB
1 9410 = 1368
8 11 r =3
0
8 1 r = 1 ← MSB 17
Decimal ↔ Hexadecimal Conversion
Decimal ↔ Hexadecimal Conversion
The Process: Successive Division
• Divide number by 16; R is the LSB of the hex
number
• While Q is 0
• Using the Q as the decimal number.
• New remainder is MSB of the hex number.
5
16 94 r = E ← LSB
S
0 9410 = 5E16
16 5 r = 5 ← MSB
Example: Hex → Octal
Example: Hex → Octal
Example:
C
Convert the hexadecimal number 5A
h h d i l b 5AH into its octal equivalent.
i i l i l
Solution:
First convert the hexadecimal number into its decimal equivalent, then
First convert the hexadecimal number into its decimal equivalent then
convert the decimal number into its octal equivalent.
11
5 A 8 90 r = 2 ← LSB
16 16
1 0 1
16 1
8 11 r =3
0
80 + 10 = 9010 8 1 r = 1 ← MSB
∴ 5AH = 1328
19
Example: Octal → Binary
Example: Octal → Binary
Example:
Convert the octal number 1328 into its binary equivalent.
Convert the octal number 132 into its binary equivalent
Solution:
First convert the octal number into its decimal equivalent, then convert the
d
decimal number into its binary equivalent.
l b b l
45
1 3 2 2 90 r = 0 ← LSB
22
82 81 80 2 45 r = 1
11
2 22 r = 0 1328 = 10110102
64 8 1
5
2 11 r = 1
2
64 + 24 + 2 = 9010 2 5 r =1
1
2 2 r =0
0 20
2 1 r = 1 ← MSB
Binary ↔ Octal ↔ Hex Shortcut
Binary ↔ Octal ↔ Hex Shortcut
• Relation
• Binary, octal, and hex number systems
Bi l dh b
• All powers of two
• Exploit (This Relation)
•Make conversion easier.
21
Substitution Code
Substitution Code
Convert 010101101010111001101010
Convert 0101011010101110011010102 to hex using
to hex using
the 4‐bit substitution code :
5 6 A E 6 A
0101 0110 1010 1110 0110 1010
56AE6A16
Substitution Code
Substitution Code
Substitution code can also be used to convert binary to
octal by using 3‐bit groupings:
2 5 5 2 7 1 5 2
010 101 101 010 111 001 101 010
255271528
Other Representation
Other Representation
• Signed & Unsigned Number
• Signed number last bit (one MSB) is signed bit
Si d b l bi ( MSB) i i d bi
Assume: 8 bit number
Unsigned 12 : 0000 1100
Signed +12 : 0000 1100
Signed ‐12 : 1000 1100
• Complement number
p
Unsigned binary 12 = 00001100
1’ss Complement of 12 = 1111
1 Complement of 12 = 1111 0011
24
Thanks
25