Uploaded by CtrlAltDel

Data Representation: Number Systems & Base Conversion

advertisement
Lecture 1
Data Representation ::
Data Representation
The term data representation means the code or
technique in which the data can be represented.
Number Systems
Number systems are very important to understand
because the design and organization of a computer
depends on the number systems. The four kind of
number system used by the digital computer –
1.Decimal number system
2.Binary number system
3.Octal number system
4.Hexadecimal number system
Data Representation
How do computers represent data?
Ø
Most computers are digital
§ Recognize only two
discrete states: on or off
§ Use a binary system to
recognize two states
§ Use number system with
two unique digits: 0 and 1,
called bits (short for
binary digits)
§ Smallest unit of data
computer can process
Data Representation
The “WHY” side
Binary numbers
All computers work with 0’s and 1’s so it is like learning
alphabets before learning English
Base conversion
For convenience, people use other bases (like decimal,
hexadecimal) and we need to know how to convert from one to
another.
Number systems
There are more than one way to express a number in binary. So
1010 could be -2, -5 or -6 and need to know which one.
Number representation
Bases we will use
• Base 2 (i.e., binary): only use 0 and 1
• Base 8 (octal): only use 0,1,…7
• Base 10 (decimal): use 0, 1, …., 9
• Base 16 (hexadecimal): use 0,1,…9, A,B,C,D,E,F
Base
base of a number system is the number of possible values
! The
for each digit.
Values for each
digit
0 to 9
0 to 1
0 to 7
0 to F
(A=10, B=11, ... F=15)
Base
?
10
2?
8?
16
?
Name of
number system
?
Denary/Decimal
Binary ?
Octal ?
Hexadecimal
?
Decimal/ denary Number System
Can you spot any patterns in how we’re counting up?
How does this pattern relate to the ‘base’?
• The decimal number system consists of 10 digits
namely 0 to 9.
• Since the decimal number system consists of 10
digits, the base or radix of this system is 10.
• e.g (23625)10 , (405)10 , (145.25)10
Decimal/ denary Number System
If we were to write out the digits of the denary number
“2493”, what is the value of each digit?
Power of 10
representation
multiply
103 102 101
100
i.e. each ‘1’ in this place
value is worth 1000.
1000 100 10
1
This subscript means
the number is in base
10. We don’t include it
if the base is obvious
from the context.
2 4 9 310
2x103 4x102 9x101 3x100
? +400
? +90
? + 3? = 2493
?
2000
Decimal/ denary Number System
If we were to write out the digits of the denary number
“145.25”, what is the value of each digit?
Power of 10
representation
multiply
102 101
100 10-1 10-2
100 10
1
0.1 0.01
1 4 5 . 2 510
This subscript means
the number is in base
10. We don’t include it
if the base is obvious
from the context.
1x102 4x101 5x100 2x10-1 5x10-2
?
100
? + ? 5 + 0.2
? + 0.05
? = 145.25
? + 40
Any Base → Denary
Now suppose we had a number in base 5 instead. How
do we convert it to denary?
53
52
51
50
?
?
125
25
5?
1?
In base 10, the place values
were powers of 10, i.e. 1,
10, 100, 1000. So in base 5,
we’d have powers of 5,
always starting with 1 on
the right.
4 3 0 15
4x53 3x52? 0x51 1x50
? + 75
? + 0? + 1? = 576?
500
Test Your Understanding
Convert the following to denary
8
4
?2
1
1 0 1 12
8 + 0 + ?2 + 1 = 11
27 9 ? 3
64 16 ? 4
1
3 3 0 24
192 + 48 + 0? + 2 = 242
1
1 2 2 03
27 + 18 ?+ 6 + 0 = 51
Convert decimal to binary
Convert 56 to binary
Convert 99 to binary
99 ÷ 2 =
49 ÷ 2 =
24 ÷ 2 =
12 ÷ 2 =
6÷2 =
3÷2 =
1÷2 =
5610=1110002
Binary
Quotient Remainder
49
1
24
1
12
0
6
0
3
0
1
0
0
1
9910=11000012
Convert decimal to octal
Convert 56 to octal
Convert 99 to octal
Binary
Quotient Remainder
99 ÷ 8 =
12
3
12 ÷ 8 =
1
4
5610 = 708
9910 = 438
Binary Number System
•
•
•
•
Base 2
Two Digits: 0, 1
Example: 10101102
Positional Number System
2
n -1
4 3 2 1 0
2 2 2 2 2
bn -1  b4 b3 b2 b1 b0
• Binary Digits are called Bits
• Bit bo is the least significant bit (LSB).
• Bit bn-1 is the most significant bit (MSB).
Binary Number System
•
nybble = 4 bits
•
byte = 8 bits
•
(short) word = 2 bytes = 16 bits
•
(double) word = 4 bytes = 32 bits
•
(long) word = 8 bytes = 64 bits
•
1K (kilo or “kibi”) = 1,024 bytes
•
1M (mega or “mebi”) = (1K)*(1K) = 1,048,576 bytes
•
1G (giga or “gibi”) = (1K)*(1M) = 1,073,741,824 bytes
Convert Binary Decimal
Convert the binary number 10101110 to decimal
Power of 2
representation
Decimal
representation
Base 2
representation
27
26
25
24
23
22 21
20
128
64
32
16
8
4
2
1
1
0
1
0
1
1
1
0
128 + 32 + 8 + 4 + 2 = 174
101011102 = 174
Convert Binary Decimal
Convert the binary number 10101110 to octal
First, convert given binary to decimal
128 + 32 + 8 + 4 + 2 = 174 à 101011102 = 174
Convert this decimal to octal form
Binary
Quotient Remainder
174 ÷ 8 =
21
6
21 ÷ 8 =
2
5
2 ÷8=
0
2
101011102 = 17410 = 2568
Example
Can you figure the decimal values for these binary values
a) 11
à3
b) 101
à5
c) 110
à6
d) 1111
à 15
e) 11011
à 27
Octal Number System
• The octal number system consists of 8 digits namely
0 to 7.
• Example: (76)8 , (55.25)8
Convert decimal to octal
Convert the decimal number 92 to octal
Decimal
Quotient Remainder
92 ÷ 8 =
11
4
11 ÷ 8 =
1
3
1 ÷8=
0
1
92 = 1348
Convert decimal to octal
Convert the binary number 1010.11 to octal
Power of 2
representation
Decimal
representation
Base 2
representation
23
22
21
20
2-1
8
4
2
1
0.5 0.25
1
0
1
0
1
8 + 2 + 0.5 + 0.25 = 10.75
(1010.11)2 = (10.75)10
2-2
1
Example
Can you figure the decimal values for these octal values
a) 21
à 17
b) 156
à 110
c) 270
à 184
d) 1164
à 628
e) 2105
à 1093
Hexadecimal Number System
• The Hexadecimal number system, popularly known as Hex system
has 16 symbols, therefore its base/radix in 16.
• The 16 symbols used in Hexadecimal system are
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
e.g (45)16 , (11A)16
Convert Hex to Decimal
Convert hex 7CF to decimal
7 = 7
C = 12
F = 15
7CF = (7 × 162) + (12 × 161) + (15 × 160)
= (7 × 256) + (12 × 16) + (15 × 1)
= 1792 + 192 + 15
= 1999
Convert Hex to Decimal
Convert hex 1DA6 to decimal
1 = 1
D = 13
A = 10
6 = 6
1DA6 = (1 × 163) + (13 × 162) + (10 × 161) + (6 × 160)
= (1 × 4096) + (13 × 256) + (10 × 16) + (6 × 1)
= 4096
= 7590
+ 3328
+ 160
+6
Convert Hex to Decimal
Convert hex E8B to decimal
E = 14
8 = 8
B = 11
E8B = (14 × 162) + (8 × 161) + (11 × 160)
= (14 × 256) + (8 × 16) + (11 × 1)
= 3584 + 128 + 11
= 3723
Example
Can you figure the decimal values for these hex values
a) 2A
à 42
b) 15F
à 351
c) A7C
à 2684
d) 11BE
à 4542
e) A10D
à 41229
Convert Hex to Decimal
Convert binary 11011 to hexa
Binary à decimal à hexa
Binary à decimal
(11011)2 = ?16
1 x 24 +1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = (27)10
Decimal à hexa
Decimal
Quotient Remainder
27 ÷ 16 =
1
11
1 ÷ 16 =
0
1
1, 11 = 1B
Convert Hex to Decimal
Convert hexa A59C to decimal
Hexa à decimal
Hexadecimal to Denary
Multiples
of 16:
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
A:
B:
C:
D:
E:
F:
0
16
32
48
64
80
96
112
128
144
160
176
192
208
224
240
Convert the following from
hexadecimal to denary:
(a) 34 (b) A3
16
1
16
3
?a
4
10 ?3b
48 +4 = 52
1
160 +3 = 163
Test Your Understanding
Multiples
of 16:
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
A:
B:
C:
D:
E:
F:
0
16
32
48
64
80
96
112
128
144
160
176
192
208
224
240
Convert 8C from hexadecimal to
denary.
16
1
8
12?
128 +12 = 140
Denary to Hexadecimal
Multiples
of 16:
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
A:
B:
C:
D:
E:
F:
0
16
32
48
64
80
96
112
128
144
160
176
192
208
224
240
We can use exactly the same method as we did for denary to
any general base…
Convert 183 from denary to hexadecimal.
16
1
B?
7?
176 + 7 = 183
What is the most number of 16s we can use to make
up 183? The table on the left will be very handy!
Test Your Understanding
Multiples
of 16:
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
A:
B:
C:
D:
E:
F:
0
16
32
48
64
80
96
112
128
144
160
176
192
208
224
240
Convert 78 from denary
to hexadecimal.
16
4
?
Convert 234 from denary
to hexadecimal.
1
16
E
E
64 + 14 =84
1
?
A
224 + 10 =234
Hexadecimal to Binary
We already have methods to convert from denary to other bases, and other
bases to denary. We could therefore convert via denary.
Convert 5A from hexadecimal to binary.
Convert to denary first:
16
1
5 10 ?
80 + 10 = 90
Then convert from denary to binary:
64 32 16 8 4 2 1
1 0 1 1 ?0 1 0
64
+16+8 +2
= 90
Hexadecimal to Binary
Convert 5A from hexadecimal to binary.
But a nice little trick to convert each digit separately (noting that each
hexadecimal digit requires 4 bits, i.e. a nibble), and then to concatenate the
results together.
5 in binary
A = 10 in binary
à 0101 ?
à 1010 ?
Therefore 5A in binary
à 0101 1010
?
Further Example
Convert B9 from hexadecimal to binary.
B = 11 in binary
9 in binary
à 1011 ?
à 1001 ?
Therefore B9 in binary
?
à 10111001
Test Your Understanding
Convert 6F from hexadecimal to binary.
6 in binary
F in binary
à 0110 ?
à 1111 ?
Therefore 6F in binary
?
à 01101111
Binary Addition
Rule for Binary Addition:
0+0=0
0+1=1
1+0=1
1 + 1 = 0 (Carry 1)
Conversion between bases
To convert from one base to another:
1) Convert the Integer Part
2) Convert the Fraction Part
3) Join the two results with a radix point
Conversion between bases
• To Convert the Integral Part:
Repeatedly divide the number by the new radix and save
the remainders. The digits for the new radix are the
remainders in reverse order of their computation. If the
new radix is > 10, then convert all remainders > 10 to
digits A, B, …
• To Convert the Fractional Part:
Repeatedly multiply the fraction by the new radix and
save the integer digits that result. The digits for the new
radix are the integer digits in order of their computation.
If the new radix is > 10, then convert all integers > 10 to
digits A, B, …
Conversion between bases
Convert 4 6. 6 8 7 510 to binary (base 2)
Convert 46 to Base 2
46/2 = 23 rem 0
23/2 = 11 rem 1
11/2 = 5 rem 1
5/2 = 2 rem 1
2/2 = 1 rem 0
1/2 = 0 rem 1
Convert 0.6875 to Base 2
0.6875 x 2 = 1.3750 int = 1
0.3750 x 2 = 0.7500 int = 0
0.7500 x 2 = 1.5000 int = 1
0.5000 x 2 = 1.0000 int = 1
0
Reading remainders in reverse:
1011102
Reading int portion in forward
direction: 0.10112
Join the results together with the radix point: 101110.10112
Convert decimal to octal
Convert the decimal number 10.75 to octal
Multiply the fractional part
repeatedly by 8 until it
becomes 0.
Whole number
Quotient Remainder
10 ÷ 8 =
1
2
1 ÷8=
0
1
10.7510 = 12.68
Fractional
0.75 x 8 =
6
0
Conversion between bases
Convert 5 6. 8 9 10 to octal (base 8)
Convert 56 to Base 8
56/8 = 7 rem 0
7/8 = 0 rem 7
Reading remainders in reverse: 708
Convert 0.89 to Base 8
0.89 x 8 = 7.12
0.12 x 8 = 0.96
0.96 x 8 = 7.68
0.68 x 8 = 5.44
0.44 x 8 = 3.52
0.52 x 8 = 4.16
0.16 x 8 = 1.28
0.28 x 8 = 2.24
………
0.8910 : 0.70753412…8
Join the results together with the radix point: 70.70753412...8
7
0
7
5
3
4
1
2
0.12
0.96
0.68
0.44
0.52
0.16
0.28
0.24
Conversion between bases
Convert 51966. 3125 10 to hexadecimal (base 16)
Convert 56 to Base 16
51966/16
= 3247
3247/16
= 202
202/16
= 12
12/16
=0
R 14
R 15
R 10
R 12
Convert 0.3125 to Base 16
0.3125 x 16 = 5.0
5
0
12 10 15 14
0.322510 : 0.516
Reading remainders in reverse: CAFE16
Join the results together with the radix point: CAFE.516
0.0
Conversion between bases
Convert 51966. 3125 10 to hexadecimal (base 16)
Convert 56 to Base 16
51966/16
3247/16
202/16
12/16
= 3247 R 14
= 202 R 15
= 12 R 10
=0
R 12
Convert 0.3125 to Base 16
0.3125 x 16 = 5.0
5
0
12 10 15 14
0.322510 : 0.516
Reading remainders in reverse: CAFE16
Join the results together with the radix point: CAFE.516
0.0
Test Your Understanding
Convert the following decimal:
11.7510
to binary
9.62510
to binary
62.510
to octal
106.4510
to octal
36.812510
to hexadecimal
3162.562510
to hexadecimal
Test Your Understanding
11.7510
to binary
Whole number
Quotient Remainder
11 ÷ 2 =
5
1
5 ÷2=
2
1
2 ÷2=
1
0
1 ÷2=
0
1
1110 = 10112
Fractional
0.75 x 2 =
0.5 x 2 =
0
0.7510 = 112
11.7510 = 1011.112
1
1
5
0
Test Your Understanding
9.62510
to binary
Whole number
Quotient Remainder
9÷2=
4
1
4 ÷2=
2
0
2 ÷2=
1
0
1 ÷2=
0
1
Fractional
0.625 x 2 =
0.25 x 2 =
0.5 x 2 =
0
910 = 10012
0.62510 = 1012
9.62510 = 1001.1012
1
0
1
25
5
0
Test Your Understanding
62.510
to octal
Whole number
Quotient Remainder
62 ÷ 8 =
7
6
7 ÷8=
0
7
0.5 x 8 =
0
6210 = 768
0.510 = 0.48
9.62510 = 76.48
Fractional
4
0
Test Your Understanding
106.4510
to octal
Whole number
Quotient Remainder
106 ÷ 8 =
13
2
13 ÷ 8 =
1
5
1 ÷8=
0
1
10610 = 1528
Fractional
0.45 x 8
0.6 x 8
0.8 x 8
0.4 x 8
0.2 x 8
0.6 x 8
0.8 x 8
…
=
=
=
=
=
=
=
3
4
6
3
1
4
6
6
8
4
2
6
8
4
0.4510 = 0.3463146…8
106.4510 = 152. 3463146…8
Test Your Understanding
36.812510
to hexadecimal
Fractional
Whole number
Quotient Remainder
0.8125 x 16= 13
0
36 ÷ 16 =
2
4
0
2 ÷ 16 =
0
2
3610 = 2416
0.812510 = 0.1316
0.812510 = 0.D16
36.812510 = 24.D16
Test Your Understanding
3162.562510
to hexadecimal
Fractional
Whole number
Quotient Remainder
0.5625 x 16 = 9
0
3162 ÷ 16 = 197 10
0
197 ÷ 16 = 12
5
12 ÷ 16 = 0
12
316210 = 12 5 1016
0.812510 = 0.916
316210 = C 5 A16
3162.562510 = C5A.916
Binary Addition-Example
Add 110101 and 101111
1
1
1
0
1
0
1
1
0
1
1
1
1
1
0
0
1
0
0
Binary Addition-Example
Add 10110 and 1101
1
1
0
1
1
0
0
1
1
0
1
0
0
0
1
1
Adding in binary
+
1
?e
1
1
1
0
?d
0
1
1
?c
0
0
1
?b
1
1
1
0
?a
Further Example
+ 1
1 0
?f
?e
1
1
1
0
?d
0
0
1
?c
1
1
1
0
?b
0
1
1
?a
Test Your Understanding
1
+
1
0
1
1
1?
0
1
0
1
1
0
+
1
1
1
0
0
0
1?
0
1
0
1
1
0
Review
1
Convert 71 from denary to binary
64
32
16
8
4
2
1
1
0
0
0
?
1
0
1
2
Add the binary numbers:
11010 + 1111
1 1 0 1 0
+ ? 1 1 1 1
1 0 1 0 0 1
Download