Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying: → → → → 10, which is a binary of the decimal 2 Example:
Exercise Answers:
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: = 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: : 127 instead of 255 before. What happens if we make the entire byte all 1’s? : -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 = However for signed numbers: -(2^b-1) to (2^b-1) - 1 byte: -(2 ⁸ -1) to (2 ⁸ ) = - 2 ⁷ to 2 ⁷ - 1 = -128 to 127 short: to 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 = = Can represent numbers from -32,767 to 32, But, still have two representations for zero: 0 = =
2's Complement Negative number obtained by taking 1's Complement of positive number and adding = 's Comp = 's Comp = Word integer can represent numbers from -32,768 to 32, Byte integer can represent numbers from -128 to One version of zero:
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 = 17.
Example: ( ) First we apply two's complement to which gives us Now we need to add , however when you do the addition you always disregard the last carry, so our example would be: which gives us , now we can convert this value into decimal, which gives So the full calculation in decimal is = (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: ( ) 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 So, the full calculation in decimal is = (correct !!) (120) ’s complement + 1 2’s complement (100) (-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.
Exercise Do subtraction for the following: – – –