Transcript Lecture 3

Representing Integer Data
( Subject has no point !! )
Book : Chapter 4
Basic Definition
• An integer is a number which has no
fractional part.
Examples:
-2022
-213
0
1
666
54323434565434
Ranges for Data Formats
No. of bits
Binary
1
0–1
2
0–3
3
0–7
4
0 – 15
5
0 – 31
6
0 – 63
7
0 – 127
8
0 – 255
9
0 – 511
16
24
Etc.
BCD
ASCII
0–9
0 – 99
0–9
0 - 65,535
0 – 9999
0 – 99
0 – 16,777,215
0 – 999999
0 – 999
In General (binary)
Binary
No. of bits
n
Min
Max
0
2n - 1
Remember !!
Signed Integers
• Previous examples were for “unsigned
integers” (positive values only!)
• Must also have a mechanism to represent
“signed integers” (positive and negative
values!)
• E.g., -510 = ?2
• Two common schemes: 1) sign-magnitude
2) two’s complement
Sign-Magnitude
• Extra bit on left to represent sign
• 0 = positive value
• 1 = negative value
• E.g., 6-bit sign-magnitude representation of
+5 and –5:
+5:
+ve
0 0 0 1 0 1
5
-5:
-ve
1 0 0 1 0 1
5
Ranges (revisited)
No. of bits
1
2
3
4
5
6
Etc.
Binary
Unsigned
Sign-magnitude
Min
Max
Min
Max
0
1
0
3
-1
1
0
7
-3
3
0
15
-7
7
0
31
-15
15
0
63
-31
31
In General (revisited)
Binary
No. of bits
n
Unsigned
Min
0
Max
n
Sign-magnitude
Min
Max
n-1
n-1
2 - 1 -(2
- 1) 2
-1
Difficulties with Sign-Magnitude
• Two representations of zero
• Using 6-bit sign-magnitude…
• 0: 000000
• 0: 100000
• Arithmetic is awkward!
pp. 95-96
Complementary Representations
• 9’s complement
• 10’s complement
• 1’s complement
Range shifting decimal integers – 9’s
complement
501
-498
Figure 4.5 Range shifting decimal integers
9’s Decimal Complement
• Taking the complement: subtracting a value from a standard basis
value
• Decimal (base 10) system diminished radix complement
• Radix minus 1 = 10 – 1
9 as the basis
• 3-digit example: base value = 999
• Range of possible values 0 to 999 arbitrarily split at 500
Numbers
Representation method
Range of decimal numbers
Calculation
Negative
Positive
Complement
Number itself
-499
-000
+0
999 minus number
Representation example
999 – 499
500
–
999
Increasing value
499
none
0
499
+
• Find the 9’s Complement representation for
the 3 digit number -467
999
-467
--------532
• Find the 9’s Complement representation for
the 4 digit number -467
9999
- 467
--------9 532
• Find the 9’s Complement representation for
the 4 digit number -3120
9999
-3120
--------6879
Addition as a counting process
Figure 4.6 Addition as a counting process
Using 9’s Complement. If we are using 4 bits, the number
5450 represents ?
9999 –
5450
---------4549
Addition with wraparound
Figure 4.7 Addition with wraparound
200 + (-300) = -100
999
-300
-----699
Addition with End-around Carry
• Count to the right crosses the modulus
• End-around carry
• Add 2 numbers in 9’s complementary arithmetic
• If the result has more digits than specified, add carry to
the result
+300
Representation
Number
represented
500
799
999
0
99
-499
-200
-000
0
100
+300
499
(1099)
499
799
300
1099
1
100
One’s complement representation
-
+
Figure 4.10 One’s complement representation
Finding one’s complement for a
negative Number
• Invert the bits !
For example one’s complement of -58 using 8 bits
0011 1010 = 58
1100 0101 = -58
Addition
• Add 2 positive 8-bit
numbers
• Add 2 8-bit numbers
with different signs
• Take the 1’s
complement of 58
(i.e., invert)
0011 1010
1100 0101
0010 1101
=
45
0011 1010
0110 0111
=
=
58
103
0010 1101
1100 0101
1111 0010
=
=
=
45
–58
–13
Invert to get
magnitude
0000 1101
8+4+1 =
13
Addition with Carry
• 8-bit number
0000 0010 (210)
1111 1101
• Add
• 9 bits
End-around carry
0110 1010 =
106
1111 1101 =
–2
10110 0111
+1
0110 1000 =
104
Subtraction
• 8-bit number
• Invert
0101 1010 (9010)
1010 0101
• Add
• 9 bits
0110 1010 =
106
-0101 1010 =
90
0110 1010 =
106
–1010 0101 =
90
10000 1111
End-around carry
+1
0001 0000 =
16
Overflow
• 8-bit number
• 256 different numbers
• Positive numbers:
0 to 127
• Add
• Test for overflow
• 2 positive inputs
produced negative
result
overflow!
• Wrong answer!
0100 0000 =
64
0100 0001 =
65
1000 0001
-126
0111 1110
Invert to get
magnitude
12610
• Programmers beware: some high-level
languages, e.g., some versions of BASIC, do not
check for overflow adequately
Ten’s complement scale
Figure 4.11 Ten’s complement scale
• What is the 3-digit 10’s complement of
247?
1000
- 247
------753
Exercises – Complementary Notations
• What is the 3-digit 10’s complement of 17?
• Answer:
• 777 is a 10’s complement representation of
what decimal value?
• Answer:
Skip answer
Answer
Exercises – Complementary Notations
Answer
• What is the 3-digit 10’s complement of 17?
• Answer:
983
• 777 is a 10’s complement representation of
what decimal value?
• Answer:
• 1000
• - 777
• 223
223
Two’s Complement
•
•
•
Most common scheme of representing
negative numbers in computers
Affords natural arithmetic (no special rules!)
To represent a negative number in 2’s
complement notation…
1.
2.
3.
4.
Decide upon the number of bits (n)
Find the binary representation of the positive value in n-bits
Flip all the bits (change 1’s to 0’s and vice versa)
Add 1
Learn!
kc
Two’s complement representation
Figure 4.12 Two’s complement representation
Two’s Complement Example
•
Represent –5 in binary using 2’s
complement notation
1. Decide on the number of bits
6 (for example)
2. Find the binary representation of the +ve value in
6 bits
000101
+5
3. Flip all the bits
111010
4. Add 1
111010
+
1
111011
-5
Sign Bit
• In 2’s complement notation, the MSB is the
sign bit (as with sign-magnitude notation)
• 0 = positive value
• 1 = negative value
+5:
0 0 0 1 0 1
+ve
5
-5:
1 1 1 0 1 1
-ve
? (previous slide)
“Complementary” Notation
• Conversions between positive and negative
numbers are easy
• For binary (base 2)…
2’s C
+ve
-ve
2’s C
Example
+5
0 0 0 1 0 1
2’s C
1 1 1 0 1 0
+
1
-5
1 1 1 0 1 1
2’s C
0 0 0 1 0 0
+
1
+5
0 0 0 1 0 1
Exercise – 2’s C conversions
• What is -20 expressed as an 8-bit binary
number in 2’s complement notation?
• Answer:
• 1100011 is a 7-bit binary number in 2’s
complement notation. What is the decimal
value?
• Answer:
Exercise – 2’s C conversions
Answer
• What is -20 expressed as an 8-bit binary number
in 2’s complement notation?
• Answer: 11101100
• 1100011 is a 7-bit binary number in 2’s
complement notation. What is the decimal value?
• Answer: -29
Detail for -20 -> 11101100
-2010: Positive Value =
00010100
“Flip”:
11101011
Add 1:
(One’s complement)
+ 1
11101100
kc
Detail for 1100011 -> - 29
2’s Complement:
1100011
“Flip”: (One’s complement)
0011100
Add One:
+
1
0011101
Converts to:
=
- 29
kc
Range for 2’s Complement
• For example, 6-bit 2’s complement notation
100000
100001
-32
-31
111111
...
Negative, sign bit = 1
-1
000000
0
000001
1
011111
...
31
Zero or positive, sign bit = 0
Ranges (revisited)
Binary
No. of
bits
1
2
3
4
5
6
Etc.
Unsigned
Min
Max
0
1
0
3
0
7
0
15
0
31
0
63
Sign-magnitude 2’s complement
Min
Max
Min
Max
-1
1
-2
1
-3
-7
-15
-31
3
7
15
31
-4
-8
-16
-32
3
7
15
31
In General (revisited)
Binary
No. of Unsigned
bits
Min Max
n
0
n
Sign-magnitude 2’s complement
Min
2 - 1 -(2
n-1
Max Min
n-1
- 1) 2 -1 -2
Hint! Learn this table!
n-1
Max
2
n-1
-1
2’s Complement Addition
• Easy
• No special rules
• Just add
What is -5 plus +5?
• Zero, of course, but let’s see
Sign-magnitude
-5:
+5:
10000101
+00000101
10001010
Twos-complement
-5:
+5:
11 1 1 1 1 11
11111011
+00000101
00000000
2’s Complement Subtraction
• Easy
• No special rules
• Just subtract, well … actually … just add!
A – B = A + (-B)
add
2’s complement of B
What is 10 subtract 3?
• 7, of course, but…
• Let’s do it (we’ll use 6-bit values)
10 – 3 = 10 + (-3) = 7
+3: 000011
1s C: 111100
+1:
1
-3: 111101
001010
+111101
000111
What is 10 subtract -3?
(-(-3)) = 3
• 13, of course, but…
• Let’s do it (we’ll use 6-bit values)
10 – (-3) = 10 + (-(-3)) = 13
-3: 111101
1s C: 000010
+1:
1
+3: 000011
001010
+000011
001101
Overflow
• the result of the calculation does not fit the
available number of bits for the result
• Ex: 1 sign bit, 2 bits for the number
010 +
010
---------------100
• Detection: sign of the result is different then
the signs of the operands
Overflow
• 8-bit number
• 256 different numbers
• Positive numbers:
0 to 127
• Add
• Test for overflow
• 2 positive inputs
produced negative
result
overflow!
• Wrong answer!
0100 0000 =
64
0100 0001 =
65
1000 0001
-126
0111 1110
Invert to get
magnitude
12610
• Programmers beware: some high-level
languages, e.g., some versions of BASIC, do not
check for overflow adequately
Carry
• the result of an addition (or subtraction)
exceeds the allocated bits, independently of
the sign
• Ex: 1 sign bit, 2 bits for the number (carry,
not overflow)
010 +
110
----------1000