Download Lecture 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Approximations of π wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Systems I: Computer
Organization and Architecture
Lecture 2: Number Systems and
Arithmetic
Number Systems - Base 10
The number system that we use is base 10:
1734 = 1000 + 700 + 30 + 4
= 1x1000 + 7x100 + 3x10 + 4x1
= 1x103 + 7x102 + 3x101 + 4x100
724.5 = 7x100 + 2x10 + 4x1 + 5x0.1
= 7x102 + 2x101 + 4x100 + 5x10-1
Why use base 10?
Number Systems - Base 2
For computers, base 2 is more convenient (why?)
100112 = 1x16 + 0x8 + 0x4 + 1x2 + 1x1 = 1910
1000102 = 1x32 + 0x16 + 0x8 + 0x4 + 1x2 + 0x1 = 3410
101.0012 = 1x4 + 0x2 + 1x1 + 0x0.5 + 0x0.25 + 1x0.125
= 5.12510
Example -
11010112 = ?
101101112 = ?
10100.11012 = ?
Number Systems - Base 16
Hexadecimal (base 16) numbers are commonly used
because it is convert them into binary (base 2) and vice
versa.
8CE16 = 8x256 + 12x16 + 14x1
= 2048 + 192 + 14
= 2254
3F9
= 3x256 + 15x16 + 9x1
= 768 + 240 + 9 = 1017
Number Systems - Base 16 (continued)
Base 2 is easily converted into base 16:
1000110011102 = 1000 1100 1110 = 8 C E 16
111011011101010012 = 1 1101 1011 1010 1001 = 1 D B A 916
101100010100000101112 = ?16
1011010100101110112 = ?16
Number Systems - Base 16 (continued)
Converting base 16 into base 2 works the same way:
F3A516 = 1111 0011 1010 01012
76EF16 = 0111 0110 1110 11112
AB3D16 = ?2
15C.3816 = ?2
Number Systems – Base 8
Octal (base 8) numbers used to be commonly used because it
is convert them into binary (base 2) and vice versa.
However, the absence of 8 and 9 is not obvious enough
and they were frequently mistaken for decimal values.
43168 = 4 x 83 + 3 x 82 + 1 x 81 + 6 x 80
= 4 x 512 + 3 x 64 + 1 x 8 + 6 x 1
= 2048 + 192 + 8 + 6
= 225410
Number Systems - Base 8 (continued)
Base 2 is easily converted into base 8:
1000110011102 = 100 011 001 110 = 4 3 1 68
111011011101010012 = 11 101 101 110 101 001 = 3556518
101100010100000101112 = ?8
1011010100101110112 = ?8
Number Systems - Base 8 (continued)
Converting base 8 into base 2 works the same way:
363518 = 11 110 011 101 001 012
733578 = 111 011 011 101 1112
24368 = ?2
15738 = ?2
Converting From Decimal to Binary
19
9R1
4R1
2R0
1R0
0R1
100112
Converting From Decimal to Hexadecimal
16
ED16
237
14 R 13
0 R 14
Converting From Decimal to Octal
8 237
29 R 5
3R5
0R3
3558
Binary, Octal, Decimal and Hexadecimal Equivalents
Binary
Decimal
Octal Hex.
Decimal
8
Octal
10
Hex.
8
0000
0
0
0
Binary
1000
0001
1
1
1
1001
9
11
9
0010
2
2
2
1010
10
12
A
0011
3
3
3
1011
11
13
B
0100
4
4
4
1100
12
14
C
0101
5
5
5
1101
13
15
D
0110
6
6
6
1110
14
16
E
0111
7
7
7
1111
15
17
F
Addition of Binary Numbers
C
190
X
+ 141
Y
331
X+Y
101111000
10111110
+ 10001101
101001011
C
127
X
+
63
Y
190
X+Y
011111110
01111111
+ 00111111
10111110
Addition of Binary Numbers (continued)
C
174
X
+ 44
Y
217
X+Y
001011000
10101101
+ 00101100
11011001
C
170
X
+ 85
Y
255
X+Y
000000000
10101010
+ 01010101
11111111
Addition of Hexadecimal
Numbers
C
X
Y
X+Y
1100
19B916
C7E616
E19F16
1
1
12
14
14
E
1
9
7
17
16+1
1
0
11
14
25
16+9
9
0
9
6
15
15
F
Complements
There are several different ways in which we
can represent negative numbers:
• Signed-Magnitude Representation
• 1s Complement Representation
• 2s Complement Representation
Signed-Magnitude Representation
• In signed-magnitude representation, the sign bit is
set to ‘1’ if negative and cleared to ‘0’ if positive:
6
00000110
+13
00001101
19
00010011
-6
+ +13
+7
10000110
00001101
10010011 ( = -19)
1s Complement Representation
• In signed-magnitude representation, the sign bit is
set to ‘1’ if negative and the other bits are also
reversed.
6
00000110
+13
00001101
19
00010011
-6
+ +13
+7
1
11111001
00001101
00000110 ( = +6)
overflow bit
2s Complement Representation
• In 2s complement representation, we subtract the
absolute value from 2n:
100000000
00000110
11111010
-6
+ +13
+7
1
11111010
00001101
00000111 ( = +7)
2s Complement Representation (continued)
• The 2s complement representation can also be
found by reversing the bits (into 1s complement)
and then adding 1:
6 => 00000110 =>11111001
+
1
11111010
43 => 00101011 => 11010100
+
1
11010101
Overflow
• If an addition operation produces a result that exceeds our
number system’s range, overflow has occurred.
• Addition of two numbers of the same sign produces
overflow; addition two numbers of opposite sign cannot
cause overflow.
-3
1101
+5 0101
+6
0110
+6 0110
+3
1 0011 = +3
+11 1011 = -5
-8
-8
-16
1000
1000
1 0000 = 0
+7
+7
+14
0111
0111
1110 = -2
Subtraction
• Subtraction works in a similar fashion, but the borrow (an
initial carry bit) is a ‘1’:
1
initial carry
+4
0100
0100
- +3 - 0011
+1100
+1
1 0001
+3
- +4
-1
-
0011
0100
1
0011
+1011
1 0001
Subtraction (continued)
+3
- -4 -7
-3
- -4
-1
-
0011
1100
1
0011
+0011
0111
1011
1100
1
1011
+0011
1111
Binary Multiplication
• Multiplication is repeated addition of the
multiplicand, where the number of additions
depends on the multiplier:
11
x 13
33
11
143
1011
1101
1011
0000
1011
1011
10001111
multiplicand
multiplier
shifted multiplicands
product
Partial Product Method
• It is more convenient to add each shifted multiplicand as it
is created to a partial product:
1011
x 1101
partial
0000
products
1011
01011
0000
001011
1011
0110111
1011
10001111
x
11
13
143
shifted
multiplicands
Partial Product Method – Another Example
-5
1011
x -3
1101
00000
shifted multiplicand
11011
111011
00000
1111011
11011
shifted and negated multiplicand
11100111
00101
00001111
Binary Division
reduced
10011
1011 11011001
1011
0101
0000
divisor
1010
0000
10100
1011
10011
1011
1000
shifted divisor
remainder
Binary Representation of Decimal Numbers
Decimal
Digit
BCD (8421) 2421
Excess-3
0
0000
0000
0011
1
0001
0001
0100
2
0010
0010
0101
3
0011
0011
0110
4
0100
0100
0111
5
0101
1011
1000
6
0110
1100
1001
7
0111
1101
1010
8
1000
1110
1011
9
1001
1111
1100
Floating Point Representations
• The floating point representation of a number has two part:
fraction and an exponent:
+6132.789 = +0.6132789 x 104
• In general, a number can be expressed as
m x re
where m is the mantissa, r is the radix and e is the
exponent.
• Because we know that computer always use binary
numbers (radix = 2), only m and e need to be represented.
• Therefore, we can represent 1001.11 using m = 01001110
and e = 000100 (because 1011.11 = +0.101111 x 2+4)
Floating Point Representations (continued)
• A floating-point number is normalized if the most
significant place in the mantissa is nonzero.
– 350 is normalized
– 00035 is not normalized.
– 00011010 is not normalized, but 11010000 is
normalized; this requires changing the exponent to 4.
• The standard method of storing exponent is
excess-64, where
– an exponent of 1000000 is zero
– an exponent of 1000011 is positive
– an exponent of 0110100 is negative.
Gray Codes
• Sometimes electromechanical applications of
digital systems (machine tools, automotive brake
systems and copiers) require a digital value that
indicates a mechanical position.
• A standard binary code may see more than one bit
change from one position to another, which could
lead to an incorrect reading if mechanical
assembly is imperfect.
Binary Code vs. Gray Code
111
000
100
000
110
001
110
001
101
010
111
011
100
011
Binary Code
110
010
Gray Code
ASCII representation of characters
•
•
•
ASCII (American Standard Code for Information Interchange) is a
numeric code used to represent characters.
All characters are represented this way including:
– words (character strings)
– numbers
– punctuation
– control characters
There are separate values for upper case and lower case characters:
A
65
z
122
blank
32
B
66
Z
90
$
52
a
97
0
48
b
98
9
57
Control Codes
• ASCII (a 7-bit code) has 27 = 128 values.
• We only need 62 for alphanumeric
characters. Even after accounting for
common punctuation, there are far more
available code values than we need. What
do we use them for?
• Control codes include DEL (for delete),
NUL (for null). STX (Start of Text), CR
(for carriage return), etc.
Error Detection Codes
• An error is a corruption of the data from its
correct state.
• There are several codes that allow use to
detect an error. These include:
– Parity
– CRC
– Checksum
Parity
• Parity is an extra bit appended to our data
which indicates whether the data bits add up
to an even (for even parity) or odd (for odd
parity) value.
Parity Generation
Message (xyz)
P(odd)
P(even)
000
1
0
001
0
1
010
1
0
011
0
1
100
1
0
101
0
1
110
1
0
111
0
1
Odd Parity
x
y
z
x
y
z
P(odd)
CRC
• CRC (Cyclic Redundancy Check) – is an
error detecting code.
• CRC can spot single-bit errors as well as
clustered error.
Checksum
• Checksum codes involve adding bytes
modulo 256.
• This allows checksums to spot one-byte
errors.
• Checksums can use other modulos which
would allow for spotting different errors as
well.