Binary Addition The simplest arithmetic operation in binary
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 + 1001100
Exercise 1. 2. 3. 4. 5. 6. 7. 101 + 101 111 + 10 100110 + 1101 01101 + 10111 1110111110 + 1010110011 100011 + 101110 + 110001 11111 + 101101 + 100100 Answers: 1. 1010 2. 1001 3. 110011 4. 100100 5. 11001110001 6. 10000010 7. 1110000
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? 1111: -127
�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.
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 = 0000 11000100 -196 = 1111 00111011 �Can represent numbers from -32, 767 to 32, 767. �-215+1. . 215 -1 �But, still have two representations for zero: � 0 = 00000000 -0 = 11111111
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. �-215. . 215 -1 �Byte integer can represent numbers from -128 to 127. �-27. . 27 -1 �One version of zero: 00000000
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.
Example: 11101011 - 0110 (23510 - 10210) � First we apply two's complement to 0110 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 13310 So the full calculation in decimal is 23510 - 10210 = 13310 (correct !!)
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.
Example: 01100100 - 01111000 (10010 - 12010) � 1’s complement and 2’s complement the bigger number which is 120 01111000 (120) 10000111 1’s complement + 1 2’s complement 1000 � Then add the result of the 2’s complement to the smaller number (100). 1000 +01100100 (100) 11101100 � 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. The sign bit stays the same. It is not 1’s complement and 2’s complement 1 1101100 1 0010011 + 1 1 0010100 (-20) Remember, 1 bit indicates a NEGATIVE sign, whereas 0 bit indicates positive. � Now we can convert this value into a negative decimal, which gives -2010 So, the full calculation in decimal is 10010 - 12010 = -2010 (correct !!)
Exercise Do subtraction for the following: 1. 2. 3. 4. 120 – 98 36 – 92 118 – 126 87 - 25
- Slides: 11