Computer Science 1000 Digital Information Arithmetic we have seen how to represent numbers in other bases how about manipulating numbers in other bases for example, how can we add two binary numbers? as it turns out, we can, using the same technique as you learned in grade school do a digit-by-digit addition of both numbers be sure to add in carry digit when necessary Example: Add 1287 to 1432 Step 1: Add the last two digits 1287 +1432 Example: Add 1287 to 1432 Step 2: Add the next two digits note that this results in a carry digit 1287 +1432 9 Example: Add 1287 to 1432 Step 3: Add the next two digits be sure to add in the carry digit 1 1287 +1432 19 Example: Add 1287 to 1432 Step 4: Add the final two digits be sure to add in the carry digit 1 1287 +1432 719 Example: Add 1287 to 1432 Step 4: Add the final two digits be sure to add in the carry digit 1 1287 +1432 2719 Adding Binary Numbers given the process for adding two decimal numbers 1 1287 +1432 2719 can we do the same thing with binary numbers? Adding Binary Numbers yes, we can add each two corresponding digits, plus carry (if any) propagate new carry digit (if any) 1011 +0010 Adding Binary Numbers in order to add single bits together, we need to remember a few conversions: Dec 0 1 2 3 Bin 0 1 10 11 Example: Step 1: Add last two digits (in binary) 1+0=1 no carry bit 1011 +0010 1 Dec 0 1 2 3 Bin 0 1 10 11 Example: Step 2: Add second-last two digits (in binary) 1 + 1 = 10 there is a carry bit 1 1011 +0010 01 1 Dec 0 1 2 3 Bin 0 1 10 11 Example: Step 3: Add second two digits (plus carry) 1+0+0=1 there is no carry bit 1 1011 +0010 101 01 1 Dec 0 1 2 3 Bin 0 1 10 11 Example: Step 4: Add first two digits 1+0=1 there is no carry bit 1 1011 +0010 1101 101 01 1 Dec 0 1 2 3 Bin 0 1 10 11 Check: 1011 +0010 _____ 1101 table shows correct computation of 11 + 2 = 13 Decimal Binary Decimal Binary 0 0000 8 1000 1 0001 9 1001 2 0010 10 1010 3 0011 11 1011 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111 Signed Integers our discussion so far has focused on non-negative integers computers permit the storage and manipulation of signed numbers, which includes both positive and negative numbers different ways for representing signed numbers sign-magnitude two's complement Signed Integers let's first consider the decimal case recall: integer is a sequence of digits padded with leading zeroes, if necessary 00001234 how do we represent negative numbers? Sign-Magnitude instead of storing a digit in the first symbol of the sequence, let's store a special character our previous example becomes: +0001234 to store a negative number, switch the symbol + : number is positive - : number is negative -0001234 note that if the number of symbols being stored remains the same, then we must give one digit up to store the negative sign Sign-Magnitude the previously described representation is often referred to as sign-magnitude first symbol: stores sign of number (+/-) remaining symbols: stores the magnitude (size) of number one of the primary advantages of sign-magnitude is its familiarity people typically represent negative numbers using a leading – although the leading + is often omitted when writing numbers, it is still familiar (e.g. most people know what +56 means) Sign-Magnitude Question: Given x symbols (digits and sign), what is the largest number that can be represented using signmagnitude? Consider: an x-symbol, sign-magnitude number has two parts first part: sign (+/-) : 1 symbol second part: magnitude: x-1 digits to make the largest number possible, set the sign to +, and the digits to 9s 4 symbols: 6 symbols: +999 +99999 Sign-Magnitude Question: Given x symbols (digits and sign), what is the largest number that can be represented using signmagnitude? Answer: 10x-1 - 1 this follows from our previous discussion, as we now only have x-1 bits with which to represent the magnitude of the number this also means that the smallest number we can store is – (10x-1 - 1), by flipping the + to a - Binary Sign-Magnitude sign magnitude works the same in binary however, when using binary, a simplifying assumption is typically made positive number: set left-most bit to zero negative number: set left-most bit to one this assumption means that the number still only requires 1s and 0s example: store 6 and -6 in binary (4 bits) 6: 0110 -6: 1110 Binary Sign-Magnitude limits on number similar to decimal case Given x symbols: Base Largest Smallest Decimal (10) 10x-1 -1 -(10x-1 -1) Binary (2) 2x-1 -1 -(2x-1 -1) Example: 3 bits: -3 ... 3 5 bits: -15... 15 Example: Given the sequence of binary digits 1010, what is the equivalent number in decimal if this is (a) an unsigned integer and (b) a signed integer? Solution (a): Recall our algorithm for converting binary digits to its decimal number 1010 = 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 = 10 Example: Given the sequence of binary digits 1010, what number is being represented if this represents (a) an unsigned integer and (b) a signed integer? Solution (b): Use the algorithm from before, but ignore the leftmost bit 1010 = 0 x 22 + 1 x 21 + 0 x 20 = 2 then use the left-most bit to determine the sign since it's 1, we know that the number is negative: -2 Sign-Magnitude although sign-magnitude is intuitive, it does suffer from several drawbacks 1) Two representations for zero: 4 bit: 0000 and 1000 this requires special case functionality in certain circumstances e.g. 0000 and 1000 must be determined to be equal Sign-Magnitude although sign-magnitude is intuitive, it does suffer from several drawbacks 2) Special code needed for negative arithmetic e.g. addition our previous addition function would not work correctly if the number is negative additional code would need to be written this may seem trivial, except that low-level arithmetic is often built into hardware Two's Complement a different way to represent signed numbers unlike sign-magnitude, all digits in two's complement represent a coefficient to be multiplied to a power of 2 in fact, the representation is nearly identical to the positive (unsigned) version, with one simple exception: the first power is assumed to be negative Recall our algorithm for converting binary digits to its actual number 1010 = 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 = 10 in two's complement, the first power is negative 1010 = 1 x –(23) + 0 x 22 + 1 x 21 + 0 x 20 = -6 Example: What is the decimal representation of the binary number 1111, assuming two's complement? 1111 = 1 x –(23) + 1 x 22 + 1 x 21 + 1 x 20 = -1 Two's Complement – Observations for positive numbers, the sign-magnitude and two's complement representation are identical negative numbers always have their left-most bit set to one in both representations e.g. 610 = 01102 for both representations remember: for two's complement, the left-most bit represents magnitude as well for negative numbers, a sequence of all 1s represents -1 in two's complement, or the largest negative number this is the opposite of sign-magnitude, where all 1s represented the smallest negative number (11112 = -710) see previous slide for example Two's Complement given x bits, what is the largest two's complement number that we can represent? Answer: 2x-1 - 1 previous slide says that positive sign-magnitude and two's complement numbers are identical therefore, largest sign-magnitude number and two's complement number are the same Two's Complement given x bits, what is the smallest two's complement number that we can represent? the answer is perhaps not as clear, as two's complement and sign-magnitude differ considerably in representation of negative numbers let's consider a specific example Recall our breakdown of a two's complement number 1111 = 1 x –(23) + 1 x 22 + 1 x 21 + 1 x 20 = -1 what should our digits be set to, to produce the smallest number? set left-most bit to one, and all others to zero Two's Complement given x bits, what is the smallest two's complement number that we can represent? Answer: -(2x-1) accomplished by setting first bit to one, and all others to zero hence, range of two's complement number with x bits is -(2x-1) ... 2x-1 - 1 Example: given an integer of size 5 bits, what would the range of the integer be if it is: a) unsigned b) signed (sign-magnitude) 0 ... 2x -1 = { 0 ... 31} - (2x-1 -1) .. (2x-1 -1) = { -15... 15} c) signed (two's complement) - (2x-1) .. (2x-1 -1) = { -16 ... 15} Sign Extension recall with unsigned numbers, we could increase number of bits of any number by adding leading zeroes e.g. 6 using 4 bits: using 8 bits: 0110 00000110 what do we do with signed numbers? Sign Extension sign-magnitude with sign-magnitude, we can also add leading zeroes, but only after the sign bit e.g. -6 4 bit: 8 bit: 1110 10000110 Sign Extension two's complement we pad the left side of the number with the left-most bit this is often referred to as sign-extension e.g. 6 positive: pad with zeroes negative: pad with ones 4 bit: 0110 8 bit: 00000110 e.g. -6 4 bit: 1010 8 bit: 11111010 Check: 11111010 = 1 x –(27) + 1 x 26 + 1 x 25 + 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 = -6