Week 11 - Number Systems
Week 11 - Number Systems
Week 11 - Number Systems
1
Integers
Number Systems
2
Sign and Magnitude
Sign and Magnitude
• One bit dedicate to sign (positive or 0 100 = + 4
negative) 1 100 = – 4
– sign: 0 = positive (or zero), 1 = negative
• Rest represent the absolute value or –7 +0
magnitude –6 1111 0000 +1
1110 0001
– three low order bits: 0 (000) thru 7 –5 +2
1101 0010
(111) –4 1100 0011+3
• Range for n bits
– +/– 2n-1 –1 (two representations for 0) –3 1011 0100+4
• Cumbersome addition/subtraction –2 1010 0101+5
1001 0110
– must compare magnitudes –1 1000 0111 +6
to determine sign of result –0 +7
3
1s Complement
1s Complement
• If N is a positive number, then the negative of N (its 1s complement or N' ) is N' =
(2n – 1) – N
– example: 1s complement of 7
24 = 10000
1 = 00001
24 –1 = 1111
7 = 0111
1000 = –7 in 1s complement form
4
1s Complement
1s Complement
• Subtraction implemented by 1s complement and then
addition
• Two representations of 0 –0 +0
– causes some complexities in addition –1 +1
1111 0000
• High-order bit can act as sign bit –2 1110 0001
+2
1101 0010
0 100 = + 4 –3
1100 0011 +3
–7 +7
5
2s Complement
2s Complement
• 1s complement with negative numbers shifted one position
clockwise
–1 +0
– only one representation for 0
–2 +1
– one more negative number 1111 0000
than positive numbers –3 1110 0001
+2
– high-order bit can act as sign bit 1101 0010
–4
1100 0011 +3
0 100 = + 4
–5 1011 0100 +4
1 100 = – 4
1010 0101
–6 +5
1001 0110
–7 1000 0111 +6
–8 +7
6
2s Complement
2s Complement
• If N is a positive number, then the negative of N (its 2s complement or N* ) is N* =
2n – N
– example: 2s complement of 7 24 = 10000
subtract7 = 0111
1001 = repr. of –7
7
2s Complement Addition and Subtraction
2s Complement Addition and Subtraction
• Simple addition and subtraction
– simple scheme makes 2s complement the virtually unanimous choice for
integer number systems in computers
4 0100 –4 1100
+3 0011 + (– 3) 1101
7 0111 –7 11001
4 0100 –4 1100
–3 1101 +3 0011
1 10001 –1 1111
8
Number Systems - Overflow
9
2s Complement Addition and Subtraction
2s Complement Addition and Subtraction
• Simple addition and subtraction
– simple scheme makes 2s complement the virtually unanimous choice for
integer number systems in computers
4 0100 –4 1100
+3 0011 + (– 3) 1101
7 0111 –7 11001
4 0100 –4 1100
–3 1101 +3 0011
1 10001 –1 1111
10
Overflow in 2s Complement Addition/Subtraction
• Overflow conditions
– add two positive numbers to get a negative number
– add two negative numbers to get a positive number
–1 +0 –1 +0
–2 1111 0000 +1 –2 1111 0000 +1
–3 1110 0001 +2 –3 1110 0001 +2
1101 0010 1101 0010
–4 1100 0011+3 –4 1100 0011+3
–5 1011 0100 +4 –5 1011 0100 +4
–6 1010 0101
+5 –6 1010 0101
+5
1001 0110 1001 0110
–7 1000 0111 +6 –7 1000 0111 +6
–8 +7 –8 +7
5 + 3 = –8 –7 – 2 = +7
11
Overflow Condition
Overflow Condition
• Overflow when carry into sign bit position is not equal to carry-out
0 1 1 1 1 0 0 0
5 0101 –7 1001
3 0011 –2 1110
–8 1000 7 10111
overflow overflow
0 0 0 0 1 1 1 1
5 0101 1101
–3
2 0010 1011
–5
7 0111 11000
no overflow –8
no overflow
12
Summary
• Integer
- Positive numbers the same.
- Difference is in how negative numbers are represented.
- 2s complement easiest to handle: one representation for zero,
slightly complicated complementation, simple addition
- Over/underflow detection.
• Fixed-point numbers
– ex: 1.14, s1.14 (2's complement)
– Absolute error/accuracy
• Floating-point numbers
– ex: 5E3, s5E3 (sign-and-magnitude)
– Relatively error/accuracy
– Implied one
13
Number Systems - Fixed and Floating-point
14
Fixed Point Numbers
... ...
15
Example
• Round → ___________
• ea(1.389) = _______________________
16
Floating Point Numbers
Exponent Mantissa
8 bits 23 bits
M x 2(E-Bias)
17
• For S4E3 floating-point format, number 11001001 has
- a mantissa (normalized) of -9
- an exponent of 4
- value (with zero bias) is -9/16 x 16 = -9
18
Summary
• Integer
- Positive numbers the same.
- Difference is in how negative numbers are represented.
- 2s complement easiest to handle: one representation for zero,
slightly complicated complementation, simple addition
- Over/underflow detection.
• Fixed-point numbers
– ex: 1.14, s1.14 (2's complement)
– Absolute error/accuracy
• Floating-point numbers
– ex: 5E3, s5E3 (sign-and-magnitude)
– Relatively error/accuracy
– Implied one
19