Download Number Systems Decimal (Base 10) Numbers

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

Infinitesimal wikipedia , lookup

History of logarithms wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Approximations of π wikipedia , lookup

Elementary mathematics wikipedia , lookup

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Number Systems
 Readings: 3-3.3.3, 3.3.5
 Problem: Implement simple pocket calculator
 Need: Display, adders & subtractors, inputs
 Display: Seven segment displays
 Inputs: Switches
 Missing: Way to implement numbers in binary
 Approach: From decimal to binary numbers
(and back)
118
Decimal (Base 10) Numbers
 Positional system - each digit position has a value
 2534 = 2*1,000 + 5*100 + 3*10 + 4*1
 Alternate view: Digit position I from the right =
Digit * 10I (rightmost is position 0)
 2534 = 2*103 + 5*102 + 3*101 + 4*100
119
Base R Numbers
 Each digit in range 0..(R-1)
 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F ...
 A = 10
 B = 11
 C = 12
 D = 13
 E = 14
 F = 15
 Digit position I = Digit * RI
 D3 D2 D1 D0 (base R) = D3*R3+D2*R2+D1*R1+D0*R0
120
Number System (Conversion to Decimal)
 Binary: (101110)2 =
 Hexadecimal: (E32)16 =
121
Conversion from Base R to Decimal
 Binary: (110101)2
 Hexadecimal: (A6)16
122
Conversion of Decimal to Binary (Method 1)
 For positive, unsigned numbers
 Successively subtract the greatest power of two
less than the number from the value. Put a 1 in the
corresponding digit position
 20=1
 21=2
 22=4
 23=8
24=16
25=32
26=64
27=128
28=256 212=4096 (4K)
29=512 213 =8192 (8K)
210=1024 (1K)
211=2048 (2K)
123
Decimal to Binary Method 1
 Convert (2578)10 to binary
 Convert (289)10 to binary
124
Conversion of Decimal to Binary (Method 2)
 For positive, unsigned numbers
 Repeatedly divide number by 2. Remainder
becomes the binary digits (right to left)
 Explanation:
125
Decimal to Binary Method 2
 Convert (289)10 to binary
126
Decimal to Binary Method 2
 Convert (85)10 to binary
127
Converting Binary to Hexadecimal
 1 hex digit = 4 binary digits
 Convert (11100011010111010011)2 to hex
 Convert (A3FF2A)16 to binary
128
Converting Decimal to Hex
 Convert to binary, then to Hex
 Convert (198)10 to Hexadecimal
129
Arithmetic Operations
Decimal:
Binary:
5 7 8 9 2
+ 7 8 9 5 6
Decimal:
1 0 1 0 1 1 1
+ 0 1 0 0 1 0 1
Binary:
5 7 8 9 2
- 3 2 9 4 6
1 0 1 0 0 1 1 0
- 0 0 1 1 0 1 0 1
130
Arithmetic Operations (cont.)
Decimal:
Binary:
2 0 1
* 2 1 4
1 0 0 1
* 1 0 1 1
131
Half Adder
Ai Bi Carry Sum
0 0
0
0
0 1
1
0
1 0
1
0
1 1
0
1
Bi
Bi
Ai
Ai
0
0
0
1
Carry = Ai Bi
Ai
Ai
Bi
Bi
0
1
1
0
Sum = Ai Bi + Ai Bi
= Ai + Bi
Ai
Sum
Bi
Half-adder Schematic
Carry
132
Full Adder
A
0
0
0
0
1
1
1
1
B
0
0
1
1
0
0
1
1
CI
0
1
0
1
0
1
0
1
CO
0
0
0
1
0
1
1
1
S
0
1
1
0
1
0
0
1
133
Full Adder Implementation
134
Multi-Bit Addition
A3
B3
A2
B2
A1
B1
A0
B0
A3 A2 A1 A0
+ B3 B2 B1 B0
A
B
CO +
CI
A
B
CO +
CI
A
B
CO +
CI
A
B
CO +
S
S
S
S
S3
S2
S1
S0
CI
135
Multi-Bit Addition in Verilog, Parameters
module uadd #(parameter WIDTH=8)
(out, a, b);
output logic [WIDTH:0]
out;
input logic [WIDTH-1:0] a, b;
always_comb begin
out = a + b;
end
endmodule
module add4 #(parameter W=22)
(out, a, b, c, d);
output logic [W+1:0] out;
input logic [W-1:0] a, b, c, d;
endmodule
136
Negative Numbers
 Need an efficient way to represent negative
numbers in binary
 Both positive & negative numbers will be strings of bits
 Use fixed-width formats (4-bit, 16-bit, etc.)
 Must provide efficient mathematical operations
 Addition & subtraction with potentially mixed signs
 Negation (multiply by -1)
137
Sign/Magnitude Representation
-7
-6
-5
+0
+1
0000
1110
0001
0010
1101
-4
1100
-3
1011
-2
1111
1010
-1
+3
0 100 = + 4
0100
+4
1 100 = - 4
0110
1000
-0
0111
+
0011
0101
1001
+2
+5
-
+6
+7
High order bit is sign: 0 = positive (or zero), 1 = negative
Three low order bits is the magnitude: 0 (000) thru 7 (111)
n-1
Number range for n bits = +/-2
-1
Representations for 0:
138
Sign/Magnitude Addition
0 0 1 0 (+2)
+ 0 1 0 0 (+4)
1 0 1 0 (-2)
+ 1 1 0 0 (-4)
0 0 1 0 (+2)
+ 1 1 0 0 ( -4)
1 0 1 0 ( -2)
+ 0 1 0 0 (+4)
Bottom line: Basic mathematics are too complex in Sign/Magnitude
139
Idea: Pick negatives so that addition works
 Let -1 = 0 - (+1):
0 0 0 0 ( 0)
- 0 0 0 1 (+1)
 Does addition work?
0 0 1 0 (+2)
+ 1 1 1 1 ( -1)
 Result: Two’s Complement Numbers
140
Two’s Complement
 Only one representation for 0
 One more negative number than positive number
 Fixed width format for both pos. & neg. numbers
-1
-2
-3
1111
1110
+0
0000
+1
0001
1101
0010
+2
+
-4
1100
0011
+3
0 011 = + 3
-5
1011
0100
+4
1 101 = - 3
-6
1010
0101
1001
-7
0110
1000
-8
0111
+5
-
+6
+7
141
Negating in Two’s Complement
 Flip bits & Add 1
 Negate (0010)2 (+2)
-1
-2
-3
+0
0000
+1
0001
1101
-4
1100
-5
1011
-6
1111
1110
0010
1010
 Negate (1110)2 (-2)
-7
0011
+3
0100
+4
0101
1001
0110
1000
-8
0111
+2
+5
+6
+7
142
Addition in Two’s Complement
0 0 1 0 (+2)
+ 0 1 0 0 (+4)
1 1 1 0 (-2)
+ 1 1 0 0 (-4)
0 0 1 0 (+2)
+ 1 1 0 0 ( -4)
1 1 1 0 ( -2)
+ 0 1 0 0 (+4)
143
Subtraction in Two’s Complement
 A - B = A + (-B) = A + B + 1
 0010 - 0110
 1011 - 1001
 1011 - 0001
144
Overflows in Two’s Complement
Add two positive numbers but get a negative number
or two negative numbers but get a positive number
-1
-2
0001
0010
1100
0100
1010
0101
1001
-7
0110
1000
-8
0111
+6
+7
5 + 3 = -8
+3
+4
+5
+1
0000
0001
1101
-4
-5
+0
1111
1110
-3
+2
0011
1011
-6
-2
+1
0000
1101
-4
-5
1111
1110
-3
-1
+0
0010
1100
1011
1010
-6
0110
1000
-8
0011
+3
0100
+4
0101
1001
-7
+2
0111
+5
+6
+7
-7 - 2 = +7
145
Overflow Detection in Two’s Complement
5
0101
-7
1001
3
0011
-2
1110
-8
7
Overflow
Overflow
5
0101
-3
1101
2
0010
-5
1011
7
-8
No overflow
No overflow
146
Adder/Subtractor
A3
B3
A
B
CO +
CI
A2
B2
A
B
CO +
CI
A1
B1
A
B
CO +
CI
A0
B0
A
B
CO +
S
S
S
S
S3
S2
S1
S0
CI
Overflow
A - B = A + (-B) = A + B + 1
147
Converting Decimal to Two’s Complement
 Convert absolute value to unsigned binary, then
fixed width, then negate if necessary
 Convert (-9)10 to 6-bit Two’s Complement
 Convert (9)10 to 6-bit Two’s Complement
148
Converting Two’s Complement to Decimal
 If Positive, convert as normal;
If Negative, negate then convert.
 Convert (11010)2 to Decimal
 Convert (01101) 2 to Decimal
149
Sign Extension
 To convert from N-bit to M-bit Two’s
Complement (N<M), simply duplicate sign bit:
 Convert (0010)2 to 8-bit Two’s Complement
 Convert (1011)2 to 8-bit Two’s Complement
150
Solving Complex Problems
 Many problems too complex to build as one
system
 Replace with communicating sub-circuits
ST
TS
Timer
TL
Traffic
Light
Controller
 Design process:
 Understand the problem
 Break problem into subsystems, identifying
connections
 Design individual subsystems.
151
Complex Problem Example
 Design a digital clock, which can
 Display the seconds, minutes and hours
 Have three inputs
 Increment hour
 Increment minute
 Reset seconds
152
Complex Problem Example (cont.)
153
Complex Problem Example (cont.)
154
Complex Problem Example (cont.)
155