Professional Documents
Culture Documents
Computers
1
Binary Numbers
1 1 0 1 0 1 1 1 1 0 0 1
1101011110012 =
= 1 × 211 + 1 × 210 + 0 × 29
+ 1 × 28 + 0 × 27 + 1 × 26
+ 1 × 25 + 1 × 24 + 1 × 23
+ 0 × 22 + 0 × 21 + 1 × 20
= 344910
2
Binary Numbers: cont.
1 1 0 1 0 1 1 1 1 0 0 1
6 5 7 1
= 65718
1 1 0 1 0 1 1 1 1 0 0 1
D 7 9
= (1 × 23 + 1 × 22 + 0 × 21 + 1 × 20) ×28
+ (0 × 23 + 1 × 22 + 1 × 21 + 1 × 20) ×24
+ (1 × 23 + 0 × 22 + 0 × 21 + 1 × 20) ×20
= D × (162)
+ 7 × (161)
+ 9 × (160)
4
Base Conversion
Decimal to binary, i.e., N10 → N2, for some
number N .
N = Q(1) ∗ 2 + R(1)
Similarly, write
1. Set Q(0) = Na
2. Compute
Q(j−1)
Q(j) = integer b
Q(j−1)
R(j) = remainder b
Until Q(j) = 0
3. Nb = Rn ∗ bn−1 + Rn−1 ∗ bn−2 + . . . + R1 ∗ b0
7
Another Example
17910 → N2
Q0 = 179
Q1 = 179 = 89 R1 = 1
2
Q2 = 89 = 44 R2 = 1
2
Q3 = 44 = 22 R2 = 0
2
Q4 = 22 = 11 R2 = 0
2
Q5 = 11 = 5 R2 = 1
2
Q6 = 5 =2 R2 = 1
2
Q7 = 2 =1 R2 = 0
2
Q8 = 1 =0 R2 = 1
2
17910 → 101100112
8
What About Other Bases?
17910 → N16
Q0 = 179
Q1 = 179 = 11 R1 = 3
16
Q2 = 11 = 0 R2 = 11
16
9
Fractions
10 = 1 × 21 + 0 × 20
10.11 = 1 × 21 + 0 × 20 + 1 × 2−1 + 1 × 2−2
2
0.62510 = 0.625 ×
2
0.625 × 2
=
2
= 1.250 × 2−1
= 1 × 2−1 + (0.250) × 2−1
= 1 × 2−1 + 0.5 × 2−2
= 1 × 2−1 + 0 × 2−2 + (0.5) × 2−2
= 1 × 2−1 + 0 × 2−2 + 1 × 2−3
= 0.1012
But note:
0.7 = 0.12 + (0.4) × 2−1
= 0.101102 + (0.4) × 2−5
= 0.1 0110 0110 0110 0110 0110 . . .
0000 carry
4562 addend
1719 augend
6281 sum
Details
0 1 . 0 . . 1 . . .
. . . 2 . . 6 . . 5 . . 4 . . .
. . . 9 . . 1 . . 7 . . 1 . . .
1 8 1 2 8 1 6 2 8 1
13
Arithmetic cont.
Required knowledge:
14
Binary Addition
0101 + 0110 =?
in detail:
0 0 . 0 . . 1 . . .
. . . 1 . . 0 . . 1 . . 0 . . .
. . . 0 . . 1 . . 1 . . 0 . . .
1 1 1 0 1 1 1 0 0 1
0+0 = 0
1+0 = 1
1 + 1 = 10
The multiplication table is simple too:
0×0 = 0
0×1 = 0
1×1 = 1
15
Finite Length Registers
10111001 or B9
11000100 or C4
———— —
01111101 or 7D
not
101111101 or 17D
17
More Finite-Length Addition:
01001000
10111000
————
00000000
001001000
010111000
————-
100000000
Proposition:
01001000
10110111
————
11111111
19
Twos Complement cont.
11111111
00000001
————
00000000
S0 is unique, so S2 is unique.
20
Twos Complement cont.
A rule of ordinary arithmetic:
S1 + S2 = 0 implies S2 = −S1.
Twos complementation:
2. Add 1.
21
Complemented Arithmetic
Complemented arithmetic works in any base
including decimal, if word length is fixed.
To 3 digits,
499
626
125
so the symbol 626
stands for the value -374
23
Twos complement numbers cont.
24
Binary Subtraction
Subtrahend: 001001
Its complement: 110110 (1)
Add 1: 000001
Negative of subtrahend: 110111 (2)
Minuend: 010110
Negative of subtrahend: 110111
(add!) Difference: 001101 (3)
001001
001101
010110
25
Carries and Overflows
0001 1 1111 -1
0010 2 1110 -2
0011 3 1 1101 -3
0110 6 1010 -6
0100 4 1100 -4
1010 -6 1 0110 +6
A+B =C
overflows the admissible number range.
If they do,
27
Carries and Overflows cont.
Overflow
28
Multiplication
29
Multiplication cont.
30
Multiplication Technique
To multiply M times N,
1. Set I = M; set P = 0.
2. For j = 0, 1, 2, ..., n-1 do the following:
3. If digit j of N is 1 then
set P = P + I;
Else do nothing;
4. Shift I left one place.
00011 * 11011 +3 * -5
11011 -15
1 1011
10001 works!
1101 * 1110
-3 * -2
1110
+6
11 10
111 0
works!
0110
Step 0: Initialization
*rmdr = dividend;
*quotient = 0;
shifts = 0;
Step 1: Normalization
Shift the divisor to the left until the leftmost 1 is just to the
right of the sign bit. Count the number of shifts.
33
For this example, the number of shifts would be 2, meaning
that we would need to perform 3 subtractions to complete
the process.
34
Observations
00101010 divisor
00000010 quotient
end of iteration 1
35
shift divisor and quotient for next iteration:
00010101 divisor
100 quotient
end of iteration 2
final iteration:
00001010 divisor
00000101 quotient
00000100 remainder
36
Shifting Left and Right
Shifting left is equivalent to multiplication by
the base – by 2 in binary notation, by 10 in
decimal.
00101 (decimal 5)
left: 01010 (decimal 10)
right: 00101 (decimal 5)
38
Sign and Magnitude
Example: 8-bits
+/- 1 0 1 1 0 0 1
0 10000000 01100110000000000000000
31 < 30 − 23 > < 22 − 0 >
40
Real Number Representation cont.
41
Real Number Representation cont.
Mant=0 Mant=0x7FFFFF
Exp = 0 0 Denornalized
Exp = 255 ∞ NAN
42
Floating Point Example
−265.7310 = −100001001.10111012
= −1.0000100110111012 × 28
Sign 1
Exponent 10000111
Mantissa 000010011011101
110000111000010011011101
Exponent: to 8 bits,
810 = 00001000
12710 = 01111111
10000111
44
ieee(S) ranges cont.
Symbol S Mantissa Exp. Real Number
45
Double Precision
63 62——–52 51——————–32
sign 11-bit exp. upper 20 mant.
31——————————————–0
lower 32-bits of mantissa
46
IEEE 754 Summary
47
Isomorphism
48
Precision
34.671 is given to 5 significant digits; frac-
tional part is given to 3.
Why?
67110 = 10100111112
In General:
na = bdb
log(na) = db log(b)
db = log(n a)
log(b)
49
Precision cont.
log(671)
db = log(671)
log(b)
=
log(2)
2.827
= 0.3011 = 9.39
50
Representational Error
3 Sources
-Measurement
-Scaling
-Truncation and Roundoff
Transducer
m easurem ent scaling
error
truncation
Digital &
Computer roundoff
A/D Conv.
Process
actuator error scaling
51
Representational Error - Integers
0.5
M axim um M axim um
representable representable
negative integer positive integer
Case 2: Truncation
A ctual error
R epresentable values
52
Representational Error - Floats
length of interval
Rep. Error =
quantization
radixn − radixn−1
=
radix(#digits in mantissa)
53
Rounding
Necessary because of quantization.
0
- ∞ + ∞
radix(#exponent digits−1)
±∞ = ±radix
Example: radix=2, # exponent bits = 8.
27 −1
±∞ = ±2 = ±2127 = ±1.7 × 1038
10110.101001
56
Rounding cont.
Observations:
57
Binary Coded Decimal
Example: 3728
58
Non-Numeric Data (Text)
59
Non-Numeric Data (Text) cont.
60
ASCII encoding
The most common coding scheme is the ASCII
(American standard for computer information
interchange) character set. In recent years
ASCII has been superceeded by ISO standards,
but it is still widely used.
1000000 100 40 @
1000001 101 41 A
1000010 102 42 B
...
1100001 141 61 a
1100010 142 62 b
1100011 143 63 b
...
1111010 172 7A z
1111011 173 7B {
...
62
Control Characters
ASCII characters 0000000 to 0011111 are non-
printable. Most of them control communica-
tion or printing.
F r e d
01000110 01110010 01100101 01100100
46 72 65 64
Upper 32 bits
A S C I
41 53 43 49
01000001 01010011 01000011 01001001
Lower 32 bits
I NUL NUL NUL
49 00 00 00
01001001 00000000 000000000 00000000
65
Alphabetic Sorting
Register length has a direct impact on the
speed of textual sorting as can be seen in the
following example.
Initial Pass 1
IN 494E AT 4154
AT 4154 IF 4946
IF 4946 IN 494E
NO 4E4F NO 4E4F
01000001 A 01100001 a
01000010 B 01100010 b
01000011 C 01100011 c
...
01011010 Z 01111010 z
67
Case Changes cont.
01100001 a
11000010 rotate left
10000101 rotate left
00001010 shift left
01000001 rotate right
3 times
68
Logical Operators
X Y X ·Y X Y X +Y
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1
69
Quasi-Numeric Data
70
Isomorphism with Integers
In the previous example, the date field of the
personnel record was stored as day-month-year.
This is chronologically bad for ordering. A bet-
ter scheme is
year-month-day,
1 7 5 6 J A 2 7
31 37 35 36 4A 41 32 37
72
Text Masking cont.
31 37 35 36 4A 41 32 37 · 00 00 00 00 F F F F 00 00
= 00 00 00 00 4A 41 00 00
31 37 35 36 4A 41 32 37 + F F F F F F F F 00 00 F F F F
= F F F F F F F F 4A 41 F F F F
73
Text Masking cont.
74
Operator Summary
Operator Operation
75