Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary numbers and arithmetic. ADDITION Addition (decimal)

Similar presentations


Presentation on theme: "Binary numbers and arithmetic. ADDITION Addition (decimal)"— Presentation transcript:

1 Binary numbers and arithmetic

2 ADDITION

3 Addition (decimal)

4 Addition (binary)

5

6 So can we count in binary?

7 Counting in binary (4 bits) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0000 0001 …

8 MULTIPLICATION

9 Multiplication (decimal)

10 Multiplication (binary)

11 It’s interesting to note that binary multiplication is a sequence of shifts and adds of the first term (depending on the bits in the second term. 110100 is missing here because the corresponding bit in the second terms is 0.

12 REPRESENTING SIGNED (POSITIVE AND NEGATIVE) NUMBERS

13 Representing numbers (ints) Fixed, finite number of bits. bitsbytesC/C++IntelSun 81char[s]bytebyte 162short[s]wordhalf 324int or long[s]dwordword 648long long[s]qwordxword

14 Representing numbers (ints) Fixed, finite number of bits. bitsIntelsignedunsigned 8[s]byte-2 7..+2 7 -1 0..+2 8 -1 16[s]word-2 15..+2 15 -1 0..+2 16 -1 32[s]dword-2 31..+2 31 -1 0..+2 32 -1 64[s]qword-2 63..+2 63 -1 0..+2 64 -1 In general, for k bits, the unsigned range is [0..+2 k -1] and the signed range is [-2 k-1..+2 k-1 -1].

15 Methods for representing signed ints. 1.signed magnitude 2.1’s complement (diminished radix complement) 3.2’s complement (radix complement) 4.excess b D-1

16 Signed magnitude Ex. 4-bit signed magnitude – 1 bit for sign – 3 bits for magnitude

17 Signed magnitude Ex. 4-bit signed magnitude – 1 bit for sign – 3 bits for magnitude

18 1’s complement (diminished radix complement) Let x be a non-negative number. Then –x is represented by b D -1+(-x) where b = base D = (total) # of bits (including the sign bit) Ex. Let b=2 and D=4. Then -1 is represented by 2 4 -1-1 = 14 10 or 1110 2.

19 1’s complement (diminished radix complement) Let x be a non-negative number. Then –x is represented by b D -1+(-x) where b = base & D = (total) # of bits (including the sign bit) Ex. What is the 9’s complement of 12389 10 ? Given b=10 and D=5. Then the 9’s complement of 12389 = 10 5 – 1 – 12389 = 100000 – 1 – 12389 = 99999 – 12389 = 87610

20 1’s complement (diminished radix complement) Let x be a non-negative number. Then –x is represented by b D - 1+(-x) where b = base D = (total) # of bits (including the sign bit) Shortcut for base 2? – All combinations used, but 2 zeros!

21 2’s complement (radix complement) Let x be a non-negative number. Then –x is represented by b D +(-x). – Ex. Let b=2 and D=4. Then -1 is represented by 2 4 - 1 = 15 or 1111 2. – Ex. Let b=2 and D=4. Then -5 is represented by 2 4 – 5 = 11 or 1011 2. – Ex. Let b=10 and D=5. Then the 10’s complement of 12389 = 10 5 – 12389 = 100000 – 12389 = 87611.

22 2’s complement (radix complement) Let x be a non-negative number. Then –x is represented by b D +(- x). – Ex. Let b=2 and D=4. Then -1 is represented by 2 4 -1 = 15 or 1111 2. – Ex. Let b=2 and D=4. Then -5 is represented by 2 4 – 5 = 11 or 1011 2. Shortcut for base 2?

23 2’s complement (radix complement) Shortcut for base 2? – Yes! Flip the bits and add 1.

24 2’s complement (radix complement) Are all combinations of 4 bits used? – No. (Now we only have one zero.) – 1000 is missing! What is 1000? Is it positive or negative? Does -8 + 1 = -7 work in 2’s complement?

25 excess b D-1 (biased representation) For pos, neg, and 0, x is represented by b D-1 + x Ex. Let b=2 and D=4. Then the excess 8 (2 4-1 ) representation for 0 is 8+0 = 8 or 1000 2. Ex. Let b=2 and D=4. Then excess 8 for -1 is 8 – 1 = 7 or 0111 2.

26 excess b D-1 For pos, neg, and 0, x is represented by b D-1 + x. Ex. Let b=2 and D=4. Then the excess 8 (2 4-1 ) representation for 0 is 8+0 = 8 or 1000 2. Ex. Let b=2 and D=4. Then excess 8 for -1 is 8 – 1 = 7 or 0111 2.

27 2’s complement vs. excess b D-1 In 2’s, positives start with 0; in excess, positives start with 1. Both have one zero (positive). Remaining bits are the same.

28 Summary of methods for representing signed ints. 1000=-8| 0000 unused

29 BINARY ARITHMETIC Signed magnitude 1’s complement 2’s complement Excess K (biased)

30 BINARY ARITHMETIC Signed magnitude

31 Addition w/ signed magnitude algorithm For A - B, change the sign of B and perform addition of A + (-B) (as in the next step) For A + B: if (A sign ==B sign ) then{ R = |A| + |B|; R sign = A sign ; } else if (|A|>|B|) then{ R = |A| - |B|; R sign = A sign ; } else if (|A|==|B|) then{ R = 0; R sign = 0; } else{ R = |B| - |A|; R sign = B sign ; } Complicated?

32 BINARY ARITHMETIC 2’s complement

33 Representing numbers (ints) using 2’s complement Fixed, finite number of bits. bitsIntelsigned 8sbyte-2 7..+2 7 -1 16sword-2 15..+2 15 -1 32sdword-2 31..+2 31 -1 64sqword-2 63..+2 63 -1 In general, for k bits, the signed range is [-2 k-1..+2 k-1 -1]. So where does the extra negative value come from?

34 Representing numbers (ints) Fixed, finite number of bits. bitsIntelsigned 8sbyte-2 7..+2 7 -1 16sword-2 15..+2 15 -1 32sdword-2 31..+2 31 -1 64sqword-2 63..+2 63 -1 In general, for k bits, the signed range is [-2 k-1..+2 k-1 -1]. So where does the extra negative value come from?

35 Addition of 2’s complement binary numbers Consider 8-bit 2’s complement binary numbers. – Then the msb (bit 7) is the sign bit. If this bit is 0, then this is a positive number; if this bit is 1, then this is a negative number. – Addition of 2 positive numbers. – Ex. 40 + 58 = 98

36 Addition of 2’s complement binary numbers Consider 8-bit 2’s complement binary numbers. – Addition of a negative to a positive. – What are the values of these 2 terms? -88 and 122 -88 + 122 = 34

37 So how can we perform subtraction?

38 Addition of 2’s complement binary numbers Consider 8-bit 2’s complement binary numbers. Subtraction is nothing but addition of the 2’s complement. – Ex. 58 – 40 = 58 + (-40) = 18 discard carry

39 Carry vs. overflow

40 Addition of 2’s complement binary numbers Carry vs. overflow when adding A + B – If A and B are of opposite sign, then overflow cannot occur. – If A and B are of the same sign but the result is of the opposite sign, then overflow has occurred (and the answer is therefore incorrect). Overflow occurs iff the carry into the sign bit differs from the carry out of the sign bit.

41 Addition of 2’s complement binary numbers class test { public static void main ( String args[] ) { byte A = 127; byte B = 127; byte result = (byte)(A + B); System.out.println( "A + B = " + result ); } #include int main ( int argc, char* argv[] ) { char A = 127; char B = 127; char result = (char)(A + B); printf( "A + B = %d \n", result ); return 0; } Result = -2 in both Java (left) and C++ (right). Why?

42 Addition of 2’s complement binary numbers class test { public static void main ( String args[] ) { byte A = 127; byte B = 127; byte result = (byte)(A + B); System.out.println( "A + B = " + result ); } Result = -2 in both Java and C++. Why? What’s 127 as a 2’s complement binary number? What is 11111110 2 ? Flip the bits: 00000001. Then add 1: 00000010. This is -2.

43 BINARY ARITHMETIC 1’s complement

44 Addition with 1’s complement Note: 1’s complement has two 0’s! 1’s complement addition is tricky (end-around-carry).

45 8-bit 1’s complement addition Ex. Let X = A8 16 and Y = 86 16. Calculate Y - X using 1’s complement.

46 8-bit 1’s complement addition Ex. Let X = A8 16 and Y = 86 16. Calculate Y - X using 1’s complement. Y = 1000 0110 2 = -121 10 X = 1010 1000 2 = -87 10 ~X = 0101 0111 2 (Note: C=0 out of msb.) Y - X = -121 + 87 = -34 (base 10)

47 8-bit 1’s complement addition Ex. Let X = A8 16 and Y = 86 16. Calculate X - Y using 1’s complement.

48 8-bit 1’s complement addition Ex. Let X = A8 16 and Y = 86 16. Calculate X - Y using 1’s complement. X = 1010 1000 2 = -87 10 Y = 1000 0110 2 = -121 10 ~Y = 0111 1001 2 (Note: C=1 out of msb.) X - Y = -87 + 121 = 34 (base 10) end around carry

49 BINARY ARITHMETIC Excess K (biased)

50 Binary arithmetic and Excess K (biased) Method: Simply add and then flip the sign bit. -1 0111 +5 1101 -- ---- +4 0100 -> flip sign -> 1100 +1 1001 -5 0011 -- ---- -4 1100 -> flip sign -> 0100 +1 1001 +5 1101 -- ---- +6 0110 -> flip sign -> 1110 -1 0111 -5 0011 -- ---- -6 1010 -> toggle sign -> 0010 (Not used for integer arithmetic but employed in IEEE 754 floating point standard.)


Download ppt "Binary numbers and arithmetic. ADDITION Addition (decimal)"

Similar presentations


Ads by Google