Representing Integer Data Book : Chapter ( Subject has no point !! ) A99ACF
Basic Definition An integer is a number which has no fractional part. Examples:
Ranges for Data Formats Etc. 0 – 9990 – – 16,777, – 990 – , – – 90 – 990 – – – – – 90 – – 73 0 – 32 0 – 11 ASCIIBCDBinaryNo. of bits
In General (binary) No. of bits Binary MinMax n 02 n - 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., = ? 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 5 -5: ve 5
Ranges (revisited) No. of bits Binary UnsignedSign-magnitude MinMaxMinMax Etc.
In General (revisited) No. of bits Binary UnsignedSign-magnitude MinMaxMinMax n02 n - 1-(2 n-1 - 1)2 n-1 - 1
Difficulties with Sign-Magnitude Two representations of zero Using 6-bit sign-magnitude… 0: : Arithmetic is awkward! pp
Complementary Representations 9’s complement 10’s complement 1’s complement
Figure 4.5 Range shifting decimal integers Range shifting decimal integers – 9’s complement
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 NumbersNegativePositive Representation methodComplementNumber itself Range of decimal numbers Calculation999 minus numbernone Representation example – Increasing value+ 999 – 499
Find the 9’s Complement representation for the 3 digit number
Find the 9’s Complement representation for the 4 digit number
Find the 9’s Complement representation for the 4 digit number
Figure 4.6 Addition as a counting process Addition as a counting process
Using 9’s Complement. If we are using 4 bits, the number 5450 represents ? 9999 –
Figure 4.7 Addition with wraparound Addition with wraparound (-300) = -100
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 (1099)
Figure 4.10 One’s complement representation 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 = = -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) = =– =– = = = =13 Invert to get magnitude
Addition with Carry 8-bit number (2 10 ) Add 9 bits End-around carry = =–2– =104
Subtraction 8-bit number Invert (90 10 ) Add 9 bits End-around carry = = =106 – = =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! Programmers beware: some high-level languages, e.g., some versions of BASIC, do not check for overflow adequately = = Invert to get magnitude
Figure 4.11 Ten’s complement scale Ten’s complement scale
What is the 3-digit 10’s complement of 247?
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
What is the 3-digit 10’s complement of 17? Answer: is a 10’s complement representation of what decimal value? Answer: Exercises – Complementary Notations Answer
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.Decide upon the number of bits (n) 2.Find the binary representation of the positive value in n-bits 3.Flip all the bits (change 1’s to 0’s and vice versa) 4.Add 1 Learn! kc
Figure 4.12 Two’s complement representation Two’s complement representation
Two’s Complement Example Represent –5 in binary using 2’s complement notation 1.Decide on the number of bits 2.Find the binary representation of the +ve value in 6 bits 3.Flip all the bits 4.Add 1 6 (for example)
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: ve +5: ve 5 ? (previous slide)
“Complementary” Notation Conversions between positive and negative numbers are easy For binary (base 2)… +ve -ve 2’s C
Example +5 2’s C -5 2’s C
Exercise – 2’s C conversions What is -20 expressed as an 8-bit binary number in 2’s complement notation? Answer: is a 7-bit binary number in 2’s complement notation. What is the decimal value? Answer:
What is -20 expressed as an 8-bit binary number in 2’s complement notation? Answer: is a 7-bit binary number in 2’s complement notation. What is the decimal value? Answer: -29 Exercise – 2’s C conversions Answer
Detail for -20 -> : PositiveValue = “Flip”: Add 1: kc (One’s complement)
Detail for > ’s Complement: “Flip”: Add One: Converts to: = - 29 kc (One’s complement)
Range for 2’s Complement For example, 6-bit 2’s complement notation Negative, sign bit = 1Zero or positive, sign bit = 0
Ranges (revisited) No. of bits Binary UnsignedSign-magnitude2’s complement MinMaxMinMaxMinMax Etc.
In General (revisited) No. of bits Binary UnsignedSign-magnitude2’s complement MinMaxMinMaxMinMax n02 n - 1-(2 n-1 - 1)2 n n-1 2 n Hint! Learn this table!
2’s Complement Addition Easy No special rules Just add
What is -5 plus +5? Zero, of course, but let’s see -5: : Sign-magnitude -5: : Twos-complement
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) = : s C: : 1 -3:
What is 10 subtract -3? 13, of course, but… Let’s do it (we’ll use 6-bit values) 10 – (-3) = 10 + (-(-3)) = : s C: : 1 +3: (-(-3)) = 3
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 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! Programmers beware: some high-level languages, e.g., some versions of BASIC, do not check for overflow adequately = = Invert to get magnitude
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)