Essence of Computation

advertisement
Arithmetics and Data Storage
1
INFOL1001 Introduction to Algorithm
10 November 2010
Goals (continued)
• Understand how to do simply binary
arithmetic
• How computers encode data
• How computers manipulate data using
the elemental commands
• How the Fetch & Execute Cycle works
2
INFOL1001 Introduction to Algorithm
10 November 2010
Binary Arithmetic
• Computer processing requires a little more
capability in binary math, notably
arithmetic.
• Let’s consider addition and subtraction …
3
INFOL1001 Introduction to Algorithm
10 November 2010
Binary Addition
• Just a few combinations are
possible:
– 02 + 0 2 = 02
– 02 + 1 2 = 12
– 12 + 0 2 = 1 2
• What about 12 + 12?
4
INFOL1001 Introduction to Algorithm
10 November 2010
Binary Addition
12 + 12 = 102
• Remember what that 10 means: it means a
zero in the right column, and a one in the left
column
• When you carry in base two, you are bringing
a power of two to the left…
5
INFOL1001 Introduction to Algorithm
10 November 2010
Binary Addition
1+ 1+
6
11102
+10112
=1410
110012
=2510
=1110
INFOL1001 Introduction to Algorithm
10 November 2010
Binary Subtraction
• Subtraction in base two is similar to
addition
– 112 – 112 = 002
– 112 – 002 = 112
– 102 – 002 = 102
• What about 102 - 12?
7
INFOL1001 Introduction to Algorithm
10 November 2010
Binary Subtraction
102 - 12 = 12
• Think about borrowing in decimal – you are really
borrowing a power of your base …
• In Base-10, we borrow 10 from the left column and
give it to the next column to the right. So …
• In Base-2, we borrow 2 from the left column and
give it to the next column to the right.
8
INFOL1001 Introduction to Algorithm
10 November 2010
Binary Subtraction
0
1
0
1
1
001100112
-000101102
0 0 0 1 1 1 01 2
9
INFOL1001 Introduction to Algorithm
=5110
=2210
=2910
10 November 2010
Binary Borrowing: A Little Trick
• Many students find it convenient to just mentally convert
the binary number to decimal, complete the subtraction,
and then convert the answer back to binary.
• Consider 102 – 12 = ?
– A one in binary is still a one in decimal
– When you borrow for the zero, you bring over a power of two, which is
equal to two
– The decimal conversion for the borrowing is 2-1, which is 1
– And 110 = 12
• Be sure to add your answer back up as a check
10
INFOL1001 Introduction to Algorithm
10 November 2010
Negative Numbers
Subtract by adding
73
-35
38
10’s complement
73
+65
138
Ignore carry
11
INFOL1001 Introduction to Algorithm
10 November 2010
Negative Numbers
Subtract by adding
73
-35
38
01001001
73
01001001
1 10111 01
35
00100011
00100110
2’s comp
11011100 flip
+1
-----------35
12
INFOL1001 Introduction to Algorithm
11011101
10 November 2010
Table 2.2
Positive and Negative Binary Numbers
Signed decimal
-128
-127
-126
…
…
…
-3
-2
-1
0
1
2
3
…
…
125
126
127
13
Hex
80
81
82
…
…
…
FD
FE
FF
00
01
02
03
…
…
7D
7E
7F
Binary
10000000
10000001
10000010
…
…
…
11111101
11111110
11111111
00000000
00000001
00000010
00000011
…
…
…
01111101
01111110
01111111
INFOL1001 Introduction to Algorithm
Unsigned decimal
128
129
130
…
…
…
253
254
255
0
1
2
3
…
…
…
125
126
127
10 November 2010
Signed Numbers
4-bit:
8H = -8 to 7H = +7
1000 to 0111
8-bit: 80H = -128 to 7F = +127
16-bit: 8000H = -32,768 to
7FFFH = +32,767
32-bit: 80000000H = -2,147,483,648 to
7FFFFFFFH = +2,147,483,647
14
INFOL1001 Introduction to Algorithm
10 November 2010
Questions
What is the two’s complement of
00101100?
What hex number represents the
decimal number -40?
15
INFOL1001 Introduction to Algorithm
10 November 2010
Computer Data
• Computers are very good
at representing data in
binary (1s and 0s)
• However, computers can
also deal with other data
types:
–
–
–
–
–
Text
Numbers
Graphics
Music
Video
• How?
16
INFOL1001 Introduction to Algorithm
10 November 2010
Storing Integer Numbers
• Convert the integer to binary
• Add a switch for positive/negative
-1910 = -101012
17
1
0
1
INFOL1001 Introduction to Algorithm
0
1
10 November 2010
Storing Floats (Real Numbers)
• Uses two memory positions:
– Store the number, without the decimal point, in one slot
– Store the number of digits to the LEFT of the decimal point in
another slot
123.456710
1234567
18
3
INFOL1001 Introduction to Algorithm
10 November 2010
Storing Strings
• Need to convert strings to ASCII (American
Standard Code for Information Interchange)
code first
• ASCII code is then converted to Base-2
equivalent:
CHAR
A
B
C
D
19
DEC
65
66
67
68
BIN
1000001
1000010
1000011
1000100
INFOL1001 Introduction to Algorithm
10 November 2010
ASCII Properties
20
INFOL1001 Introduction to Algorithm
10 November 2010
Questions?
21
INFOL1001 Introduction to Algorithm
10 November 2010
Download