cse1301 computer programming lecture 32: number representation
DESCRIPTION
Topics Representing characters and integers Converting binary to decimal Converting decimal to binary Adding binary numbers LimitationsTRANSCRIPT
1
CSE1301Computer Programming
Lecture 32:Number Representation
2
Topics
• Representing characters and integers• Converting binary to decimal• Converting decimal to binary• Adding binary numbers• Limitations
3
Representing Characters
• ASCII encoding (American Standard Code for Information Interchange)
• Each character represented by a one-byte (8-bit) number
• Other representations possible too:– EBCDIC (used by older IBM machines)– Unicode (16-bit character codes, provides
enough codes for characters from all modern languages)
4
Representing Integers
• We represent integers using a base-10 positional notation.
• So the number 80710 means:8104 + 0103 + 7102 + 1101 + 0100
5
Representing Integers
• Computers represent integers using a base-2 positional notation.
• So the number 101011 means:
125 + 024 + 123 + 022 + 121 + 120
132 + 016 + 18 + 04 + 12 + 11
= 43 in decimal
6
Representing Integers
• Curiously, ye Olde Englishe pub-keepers used the same system:
• So the number 101011 means:
1gallon + 0pottle + 1quart + 0pint + 1chopin + 1gill
14.6l + 02.3l + 11.1l + 00.6l + 10.3l + 10.1l
= 6.1 litres
7
Representing Integers• The first few binary numbers are:
0000....................00001....................10010....................20011....................30100....................40101....................50110....................60111....................71000....................81001....................91010....................101011....................111100....................121101....................131110....................141111....................15
8
Converting binary to decimal
Example: Convert the unsigned binary number 10011010 to decimal.
01011001
9
Converting binary to decimal
01011001
2021222324252627
01234567
10
Converting binary to decimal
0101100101234567
64 12481632128
11
Converting binary to decimal
0101100101234567
64 12481632128
128 + 16 + 8 + 2 = 154 (or 27 + 24 + 23 + 21)
So, 10011010 in unsigned binary is 154 in decimal.
12
Converting Decimal to/from Binary
Analogy: Giving a $69.10 change from largest to smallest denomination:
69.10 = 1 $50 bill + 19.10
19.10 = 1 $10 bill + 9.10
9.10 = 1 $5 bill + 4.10
4.10 = 2 $2 coin + 0.10
0.10 = 1 10c coin
13
25 20212223242627
01234567
Converting Decimal to/from Binary
Bit position.
Decimal value.
14
1248163264128
01234567
Converting Decimal to/from Binary
Bit position.
Decimal value.
15
Decimal to Binary
Example: Convert the decimal number 105 to unsigned binary.
16
Q. What is the highest power of 2 that is less than or equal to 105?
A. 64
164 12481632
0123456
Next, consider the difference: 105 - 64 = 41
128
17
1
Q. What is the highest power of 2 that is less than or equal to 41?
A. 32
11248163264
0123456
Next, consider the difference: 41 - 32 = 9
18
1
Q. What is the highest power of 2 that is less than or equal to 9?
A. 8
1 11248163264
0123456
Next, consider the difference: 9 - 8 = 1
19
1
Q. What is the highest power of 2 that is less than or equal to 1?
A. 1
11 11248163264
0123456
Next, consider the difference: 1 - 1 = 0 Done!
20
1
Finally, fill the empty boxes with zeros.
1001011248163264
0123456
So, 105 decimal is 1101001 in unsigned binary.
21
Converting decimal to binary
• We've seen that you convert binary to decimal by multiplying and adding.
• So it's no surprise that you convert decimal to binary by dividing and subtracting.
• You can also convert decimal to binary by dividing and noting the remainder.
22
Converting decimal to binary123 ÷2
61 remainder 1 ÷2
30 remainder 1 ÷2
15 remainder 0 ÷2
7 remainder 1 ÷2
3 remainder 1 ÷2
1 remainder 1 ÷2
0 remainder 1
23
Converting decimal to binary123 ÷2
61 remainder 1 ÷2
30 remainder 1 ÷2
15 remainder 0 ÷2
7 remainder 1 ÷2
3 remainder 1 ÷2
1 remainder 1 ÷2
0 remainder 1
24
Converting decimal to binary123 1111011 ÷261 remainder 1 ÷230 remainder 1 ÷215 remainder 0 ÷27 remainder 1 ÷23 remainder 1 ÷21 remainder 1 ÷20 remainder 1
25
Converting decimal to binary102 ÷2
51 remainder 0 ÷2
25 remainder 1 ÷2
12 remainder 1 ÷2
6 remainder 0 ÷2
3 remainder 0 ÷2
1 remainder 1 ÷2
0 remainder 1
26
Converting decimal to binary102 ÷2
51 remainder 0 ÷2
25 remainder 1 ÷2
12 remainder 1 ÷2
6 remainder 0 ÷2
3 remainder 0 ÷2
1 remainder 1 ÷2
0 remainder 1
27
102 1100110 ÷2
51 remainder 0 ÷2
25 remainder 1 ÷2
12 remainder 1 ÷2
6 remainder 0 ÷2
3 remainder 0 ÷2
1 remainder 1 ÷2
0 remainder 1
Converting decimal to binary
28
102 1100110 ÷251 remainder 0 ÷225 remainder 1 ÷212 remainder 1 ÷26 remainder 0 ÷23 remainder 0 ÷21 remainder 1 ÷20 remainder 1
Converting decimal to binary
Most significant
bit
29
102 1100110 ÷251 remainder 0 ÷225 remainder 1 ÷212 remainder 1 ÷26 remainder 0 ÷23 remainder 0 ÷21 remainder 1 ÷20 remainder 1
Converting decimal to binary
Least significant
bit
30
Adding binary numbers• For individual bits there are only four
possibilities:
0 0 1 1 + 0 + 1 + 0 + 1
0 1 1 10
31
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos:
11100101+ 1011101
32
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos.
1 11100101
+ 10111010
33
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos: 1
11100101+ 1011101
10
34
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos:
1 1 11100101
+ 1011101010
35
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos: 1 1 1
11100101+ 1011101
0010
36
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos: 1 1 1 1
11100101+ 1011101
00010
37
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos:
1 1 1 1 1 11100101
+ 1011101000010
38
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos: 1 1 1 1 1 1
11100101+ 1011101
1000010
39
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos: 1 1 1 1 1 1 1
11100101+ 1011101101000010
40
Adding binary numbers• For multiple digits we do the same as in
base-10: we add corresponding bits and carry the twos:
11100101 229
+ 1011101 + 93101000010 322
41
Adding binary numbers• But that only works for unsigned numbers.
(positive numbers)
• For signed numbers it's much more complicated.
42
Limitations of representations• None of these number representations acts
exactly like the integers.
• Infinitely many integers, but only 2N binary numbers for a specific N-bit representation.
• That means some operations will overflow.
• If the program doesn't crash when that happens, it will simply produce wrong answers (which is worse!)
43
Limitations of representations• Computer arithmetic is only an
approximation of integer arithmetic.
• Many of the arithmetic laws you're used to don't always hold. (See Tutorial 11 - Advanced Exercise for examples.)
44
Reading• Brookshear: Sections 1.4 to 1.7
• D&D: Appendix E