300 likes | 310 Views
Learn about binary addition, binary subtraction, 2's complement, overflow, and absolute value in computer programming. Explore concepts of positional number systems and binary conversion methods.
E N D
CSCI206 - Computer Organization & Programming Integer Addition and Subtraction Revised by Alexander Fuchsberger and Xiannong Meng in spring 2019 based on the notes by other instructors. zyBook: 10.1, 10.2
Review Positional Number Systems n-th digit Base
Binary to Decimal (b=2) 110011 543210 Digit count (n), or position
Decimal to Binary (b=2) (left to right) 42 = ?? • First power of two <= N is the first 1, e.g., 2^5 which is 32 • Subtract the value from the original, e.g., 42 - 32 == 10 • Repeat for remaining digits • insert zero at kth bit if 2^k greater than remaining value • else insert 1 and subtract value
Decimal to Binary (b=2) (left to right) insert 0’s in where the value is too small, e.g., 42-32 == 10, no 2^4 == 16 b/c 16 > 10 42 = 101010 5 4 3 2 1 0 first power of 2 <= 42 subtract, have 10 remaining next power of 2 <= 10, subtract 2 remain 2 remains, 2^1 is the last 1
Decimal to Binary (b=2) (right to left) • Let N be the base 10 number • while N > 0 • If N is odd (N%2==1) • add a 1 • else • add a 0 • N = N / 2 (integer divide)
Decimal to Binary (b=2) (right to left) 42 = even 0, 42/2 = 21R0 21 = odd 1, 21/2 = 10R1 10 = even 0, 10/2 = 5R0 5 = odd 1, 5/2 = 2R1 2 = even 0, 2/2 = 1R0 1 = odd 1, 1/2 = 0R1
Decimal to Binary (b=2) (right to left) 42 = even 0, 42/2 = 21R0 21 = odd 1, 21/2 = 10R1 10 = even 0, 10/2 = 5R0 5 = odd 1, 5/2 = 2R1 2 = even 0, 2/2 = 1R0 1 = odd 1, 1/2 = 0R1 Generate result right to left: 101010
Binary addition (zyBook 10.2) • same as elementary base-10 arithmetic 1 529 + 742 1271 Sum each place value from right to left and add carry bits if result requires an additional digit.
Binary Addition 101101 + 1110 1
Binary Addition 101101 + 1110 11
Binary Addition 1 101101 + 1110 011
Binary Addition 1 101101 + 1110 011
Binary Addition 1 101101 + 1110 011
Binary Addition 1 1 101101 + 1110 1011
Binary Addition 1 1 101101 + 1110 11011
Binary Addition 1 1 101101 + 1110 111011
Binary Addition 101101 = 45 + 1110 = 14 111011 = 59
Ripple Carry Adder • Add bits sequentially • Carry out goes into next carry in ALU Symbol
Ripple Carry Adder 1001 + 0101 1110 1 0 0 1 0 0 1 1 • Simple to understand, but slow because the result has to be computed 1 bit at a time • Faster, more complex, circuits exist! 0 0 0 1 0 1 1 1 0
Subtraction • 2’s complement negation is done with bitwise inverse plus one: • Easily implemented by computing the bitwise inverse and setting carry in to be 1 on bit 0, i.e., Bi = not(yi)
Multiplexer (Mux) By Lenehey at en.wikipedia Later version(s) were uploaded by Fresheneesz at en.wikipedia. (Transferred from en.wikipedia) [Public domain], from Wikimedia Commons
Overflow • Only possible when signs are the same of both operands. • Differing signs always produce a smaller absolute value. • Same signs produce a larger absolute value. • Overflow if positive + positive = negative or negative + negative = positive. • carry out of MSB (most significant bit) is not a valid test for overflow!
Absolute Value if (x > 0) x else -x
Absolute Value if (x > 0) x else -x
Absolute Value 0 1
Absolute Value Overflow? • if result is negative • only a problem with the C standard library implementation NAME abs, labs, llabs, imaxabs - compute the absolute value of an integer SYNOPSIS #include <stdlib.h> int abs(int j); Result of abs is a signed integer!
Consider the representations … Assuming an 8-bit integer INT_MIN : 1000 0000 // -128 INT_MAX: 0111 1111 // +127 If unsigned, 1000 0000 == 128 If signed, 2’s comp: 0111 1111 + 1 = 1000 0000 because it is signed, it is now -128
Absolute Value Overflow? • the invert plus 1 will overflow if the inverted value is all 1’s • 1000000...0 is the most negative 2’s complement value (INT_MIN) • Detect if the number is negative and its absolute value is still negative! (INT_MIN!)