Integer Data Types data type Integer Representation 1

  • Slides: 14
Download presentation
Integer Data Types data type Integer Representation 1 a collection of values together with

Integer Data Types data type Integer Representation 1 a collection of values together with the definitions of a number of operations that can be performed on those values We need to provide support for a variety of data types. For integer values, we need to provide a variety of types that allow the user to choose based upon memory considerations and range of representation. For contemporary programming languages, we would expect: - signed integers and unsigned integers - 8 -, 16 -, 32 - and (perhaps) 64 -bit representations - the common arithmetic operations (addition, subtraction, multiplication, division, etc. ) - sensible handling of issues related to limited ranges of representation - sensible handling of computational errors resulting from abuse of operations CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Unsigned Integers Integer Representation 2 We store the number in base-2, using a total

Unsigned Integers Integer Representation 2 We store the number in base-2, using a total of n bits to represent its value. Common values for n include 8, 16, 32 and 64, although any positive number of bits would work. The range of represented values will extend from 0 to 2^n – 1. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Addition of Unsigned Integers Integer Representation 3 The most obvious approach would seem to

Addition of Unsigned Integers Integer Representation 3 The most obvious approach would seem to be just like in elementary school, just add and carry: 0000 0101 : 5 + 1011 1101 : 189 ----1100 0010 : 194 But, what to do if the addition of the left-most digits results in a non-zero carry? That's wasn't an issue in elementary school since there was no limit on how many digits an integer may have. But at the hardware level we have a limited number of bits, whether it's 8 or 16 or 32 or 64 or something larger. The bottom line is that there's nowhere to put the result of the final carry-out. This results in an overflow condition, one in which the correct result cannot be represented exactly in the allowed number of bits. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Signed Integers Integer Representation 4 Issue: how do we represent the sign of the

Signed Integers Integer Representation 4 Issue: how do we represent the sign of the integer? Most obvious idea: use the left-most bit to represent the sign (1 for negative, 0 otherwise). Alas, there are consequences. If we just use the add and carry algorithm: 1 000 0101 + 0 011 1101 ----? 100 0010 : -5 : +61 : ? ? ? How do we efficiently determine the sign of the sum if the operands have opposite signs? And, are the bits for the resulting value even right? CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

2's Complement Representation Integer Representation 5 For non-negative integers, represent the value in base-2,

2's Complement Representation Integer Representation 5 For non-negative integers, represent the value in base-2, using up to n – 1 bits, and pad to 32 bits with leading 0's: 42: 101010 --> 0010 1010 For negative integers, take the base-2 representation of the value (ignoring the sign) pad with 0's to n – 1 bits, invert the bits and add 1: -42: 101010 --> 0010 1010 --> 1101 0101 --> 1101 0110 Weird! What's the point? Well, we've represented -42 in such a way that if we use the usual add/carry algorithm we'll find that 42 + -42 yields 0 (obviously desirable): 42: -42: sum: CS@VT August 2009 0010 1101 0110 0000 (ignore carry-out) Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

2's Complement Observations Integer Representation 6 To negate an integer, with one exception*, just

2's Complement Observations Integer Representation 6 To negate an integer, with one exception*, just invert the bits and add 1. 25985: 0110 0101 1000 0001 -25985: 1001 1010 0111 1111 --25985: 0110 0101 1000 0001 The sign of the integer is indicated by the leading bit. There is only one representation of the value 0. The range of representation is asymmetrical about zero: minimum maximum * QTP CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

2's Complement Shortcut Integer Representation 7 To negate an integer, with one exception*, find

2's Complement Shortcut Integer Representation 7 To negate an integer, with one exception*, find the right-most bit that equals 1 and then invert all of the bits to its left: 3328: 0000 1101 0000 -3328: 1111 0000 Why does this work? CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

2's Complement to base-10 Integer Representation 8 If the integer is non-negative, just expand

2's Complement to base-10 Integer Representation 8 If the integer is non-negative, just expand the positional representation: 0000 1101 0000 = = 2^11 + 2^10 + 2^8 3328 If the integer is negative, take its negation (in 2's complement), expand the positional representation for that, and then take the negation of the result (in base-10). CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

base-10 to 2's Complement Integer Representation 9 Obvious method: - apply the division-by-2 algorithm

base-10 to 2's Complement Integer Representation 9 Obvious method: - apply the division-by-2 algorithm discussed earlier to the magnitude of the number - if value is negative, negate the result CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

base-10 to 2's Complement Integer Representation 10 Alternate method: - find the largest power

base-10 to 2's Complement Integer Representation 10 Alternate method: - find the largest power of 2 that's less than the magnitude of the number - subtract it from the magnitude of the number and set that bit-position to 1 - repeat until the magnitude equals 0 - if value is negative, negate the result 0 1 1 2 2 4 3 8 4 16 5 32 6 64 . . . 10 1024 11 2048 12 4096 set bit 3328: 11 1280: 10 256: 8 0! CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

2's Complement Subtraction Integer Representation 11 Logically, subtraction just requires negating the second operand

2's Complement Subtraction Integer Representation 11 Logically, subtraction just requires negating the second operand then adding. So, to perform 42 – 17, we could just: 42: 17: 0010 1010 0001 --> 0010 1110 1111 ----0001 1001 add result is 25 Thus, we don't need special hardware for subtraction (as we would if we used the simple borrow/subtract algorithm). CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

2's Complement Sign Extension Integer Representation 12 In order to add or subtract two

2's Complement Sign Extension Integer Representation 12 In order to add or subtract two operands of different bit-lengths, we must first pad the shorter operand to the same length as the longer one. However, we must also preserve the sign of the operand, so we cannot just pad with leading zeroes; rather, we must replicate the leading bit: 17: 0001 --> 0000 0001 -17: 1110 1111 --> 1111 1110 1111 CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Overflow Integer Representation 13 For unsigned integers, overflow is indicated if there is a

Overflow Integer Representation 13 For unsigned integers, overflow is indicated if there is a carry-out from the left-most position. For signed integers, overflow is indicated if: - the operands have the same sign and the result differs from them in sign, or - the carry-in and carry-out associated with the left-most position differ 93: 44: 0101 1101 0010 1100 ----1000 1001 add result is negative! Both cases are easy to detect in hardware… but not always logically significant. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Overflow Integer Representation 14 Overflow cannot occur when: - adding operands of opposite signs

Overflow Integer Representation 14 Overflow cannot occur when: - adding operands of opposite signs - subtracting operands of the same sign Consider computing A + B or A – B where A and B are signed values. Can overflow occur when A is 0? Can overflow occur when B is 0? CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens