Computer Organization COMP 210 Integers Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Consequences of overflow Using shifts to perform power-of-2 multiply/divide
C Puzzles Answers on the last slide Assume machine with 32 bit word size, two’s complement integers For each of the following C expressions, either: Argue that is true for all argument values Give example where not true x < 0 ((x*2) < 0) ux >= 0 x & 7 == 7 (x<<30) < 0 ux > -1 x > y -x < -y x * x >= 0 x > 0 && y > 0 x + y > 0 x >= 0 -x <= 0 x <= 0 -x >= 0 Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y;
Real Life Ariane 5 – European missile On June 4, 1996 an unmanned Ariane 5 rocket launched by the European Space Agency exploded just forty seconds after its lift-off from Kourou, French Guiana. first voyage, a decade of development costing $7 billion. rocket and its cargo were valued at $500 million Cause: a 64 bit floating point number relating to the horizontal velocity of the rocket with respect to the platform was converted to a 16 bit signed integer. The number was larger than 32,767, the largest integer storeable in a 16 bit signed integer, the conversion failed. See http://www-users.math.umn.edu/~arnold/disasters/ariane.html
How do we get to bits? We write programs in an abstract language like C How do we get to the binary representation? Compilers & Assemblers! We write x = 5. The compiler changes it into mov 5, 0x005F The assembler changes it into: 80483c7: 8b 45 5F Compiler figures out that this is an integer and changes it into 2’s Complement
Negative numbers Problem: how do we represent negative numbers? Sign-magnitude. Wastes space (range decreases) Not used anymore (was used on a few IBM mainframes in the dark ages) 2’s complement Space efficient Arithmetic works well
The structure of a signed integer (if we’re using sign-magnitude) This is no longer used!
Instead of using sign magnitude… We’ll break the number line in the middle
…and shift the right part of the number line to the left side to get… two’s complement …and shift the right part of the number line to the left side to get… Note that negative numbers start with a 1, but starting with a 1 is not what makes the number negative. Result is called 2’s complement. There is an easy arithmetic way to convert numbers to their negative equivalent with 2’s complement.
This is the absolute value of 110 2’s complement Rule: To find the value represented by a binary number that starts with a 1 (i.e., a negative number) Take the complement of the binary number. Add 1 to the result. determine the decimal value To find the value represented by 110 Take the Complement: 001 add 1 to the complement] + 1 find the decimal value 010 = +2 To find the value represented by a binary number that starts with a 0 (i.e., a positive number) Do nothing! This is the absolute value of 110
2’s complement range There will be more negative numbers than positive numbers since 0 takes a positive space:
The signed integers for a four-bit cell
2’s complement range Range for any cell size: Smallest number is 1000… 0 Largest number is 0111…..1 The magnitude of the smallest negative number is 1 greater than the magnitude of the largest positive number. Example: 6-bit cell: -32 to 31
Encoding Integers Unsigned Two’s Complement Sign Bit short int x = 15213; short int y = -15213; Sign Bit C short 2 bytes long Sign Bit For 2’s complement, most significant bit indicates sign 0 for nonnegative 1 for negative
Numeric Ranges Unsigned Values UMin = 0 UMax = 2w – 1 000…0 UMax = 2w – 1 111…1 Two’s Complement Values TMin = –2w–1 100…0 TMax = 2w–1 – 1 011…1 Other Values Minus 1 111…1 Values for W = 16
Values for Different Word Sizes C Programming #include <limits.h> K&R App. B11 Declares constants, e.g., ULONG_MAX LONG_MAX LONG_MIN Values platform-specific Observations |TMin | = TMax + 1 Asymmetric range UMax = 2 * TMax + 1