Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:

Similar presentations


Presentation on theme: "Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:"— Presentation transcript:

1 Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying: 0 + 0 → 0 0 + 1 → 1 1 + 0 → 1 1 + 1 → 10, which is a binary of the decimal 2 Example: 111001 + 10011 111001 + 10011 1001100

2 Exercise 1. 101 + 101 2. 111 + 10 3. 100110 + 1101 4. 01101 + 10111 5. 1110111110 + 1010110011 6. 100011 + 101110 + 110001 7. 11111 + 101101 + 100100 Answers: 1. 1010 2. 1001 3. 110011 4. 100100 5. 11001110001 6. 10000010 7. 1110000

3 Representing negative numbers: One way to represent negative numbers would be to make the left most bit a sign bit. if the bit is 1, the number is negative, if the bit is 0 the number is positive! So let's see what 15 and -15 looks like: 15: 10001111 -15 = 00001111 Pretty clear distinction right? But here’s the thing. By using that bit for sign, now the maximum positive number we can represent is for 7 bits only: 01111111: 127 instead of 255 before. What happens if we make the entire byte all 1’s? 11111111: -127

4 For unsigned numbers, we can represent, where bits = b, 2^b - 1 so for a byte: it's 2 ⁸ - 1 = 255 for 2 bytes or a short: 2¹ ⁶ - 1 = 65,535 for 4 bytes or an int(on 32 bit systems): 2³² - 1 = 4294967295 However for signed numbers: -(2^b-1) to (2^b-1) - 1 byte: -(2 ⁸ -1) to (2 ⁸ -1 - 1) = - 2 ⁷ to 2 ⁷ - 1 = -128 to 127 short: -32768 to 32767 and so on.

5 1's Complement Negative number is stored as bit-wise complement of corresponding positive number. Leftmost bit of positive number is 0. That of negative number is 1. 196 = 00000000 11000100 -196 = 11111111 00111011 Can represent numbers from -32,767 to 32,767. -2 15 +1.. 2 15 -1 But, still have two representations for zero: 0 = 00000000 00000000 -0 = 11111111 11111111

6 2's Complement Negative number obtained by taking 1's Complement of positive number and adding 1. 6713 = 00011000 00011101 1's Comp = 11100111 11100010 2's Comp = 11100111 11100011 Word integer can represent numbers from -32,768 to 32,767. -2 15.. 2 15 -1 Byte integer can represent numbers from -128 to 127. -2 7.. 2 7 -1 One version of zero: 00000000 00000000

7 Subtracting binary numbers (Sign Binary Addition) The most common way of subtracting binary numbers is done by first taking the second value (the number to be subtracted) and apply what is known as two's complement, this is done in two steps: 1. complement each digit in turn (change 1 for 0 and 0 for 1). – 1’s complement 2. add 1 (one) to the result. – 2’s complement By applying these steps you are effectively turning the value into a negative number, and as when dealing with decimal numbers, if you add a negative number to a positive number then you are effectively subtracting to the same value. In other words 25 + (-8) = 17, which is the same as writing 25 - 8 = 17.

8 Example: 11101011 - 01100110 (235 10 - 102 10 ) First we apply two's complement to 01100110 which gives us 10011010. Now we need to add 11101011 + 10011010, however when you do the addition you always disregard the last carry, so our example would be: which gives us 10000101, now we can convert this value into decimal, which gives 133 10 So the full calculation in decimal is 235 10 - 102 10 = 133 10 (correct !!)

9 Negative numbers The previous example is subtracting a smaller number from a larger number. If you want to subtract a larger number from a smaller number (giving a negative result), then the process is slightly different. Usually, to indicate a negative number, the most significant bit (left hand bit) is set to 1 and the remaining 7 digits are used to express the value. In this format the MSB is referred to as the sign bit.

10 Example: 01100100 - 01111000 (100 10 - 120 10 ) 1’s complement and 2’s complement the bigger number which is 120 Then add the result of the 2’s complement to the smaller number (100). 1’s complement and 2’s complement the result of the addition leaving the most significant bit (MSB) which is the sign bit as it is which is 1. Now we can convert this value into a negative decimal, which gives -20 10 So, the full calculation in decimal is 100 10 - 120 10 = -20 10 (correct !!) 01111000 (120) 10000111 1’s complement + 1 2’s complement 10001000 10001000 +01100100 (100) 11101100 1 1101100 1 0010011 + 1 1 0010100 (-20) The sign bit stays the same. It is not 1’s complement and 2’s complement Remember, 1 bit indicates a NEGATIVE sign, whereas 0 bit indicates positive.

11 Exercise Do subtraction for the following: 1. 120 – 98 2. 36 – 92 3. 118 – 126 4. 87 - 25


Download ppt "Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:"

Similar presentations


Ads by Google