How to Perform Two’s Complement Overview: Remember, we are trying to convince ourselves that we can encode information using binary switches. Specifically, we are trying to encode numbers. We saw that if we choose a simple, well-behaved integer, like 23, we can convert this base ten number to a base two binary number. But the skeptic in us said: what if the number isn’t so well behaved? What if the number is, goodness forbid, a nasty NEGATIVE number? Then what? And the answer is: we use an encoding scheme called Two’s Complement. Things to know about two’s complement: When we are finished, we will have an 8-bit number. We actually are only allowed 7 bits, because the first bit is a sign bit. The first bit will be a 1 if the sign is negative, and a 0 if the sign is positive. (There is an interesting question that we won’t discuss – what if your encoded number won’t fit in 7 bits? For now, be reassured, we will guarantee a fit here in N301!) Here’s the only other thing you need to know: if the number is POSITIVE, you encode the number just like you did with a well behaved positive integer like 23, and then slam a 0 in the front. If the number is a negative number, though, you encode it as though it were positive, and then perform a silly little two step dance: you flip all the bits, and then add 1, and then slam a 1 in the front. That takes you from decimal, base ten format to two’s complement format. What if you have to go the other way? That is, your number is already in two’s complement, and you want to get back to base ten, decimal. What do you do? You observe the first bit in what is guaranteed to be an 8 bit number. If the first bit is a 0 (meaning the number is positive), you just decode the number as before. If the first bit is a 1 (meaning the number is negative), you have to reverse the two’s complementation before you convert bases. So, you subtract one, then flip the bits. Then, you translate to base ten. Don’t forget to throw the negative sign in front of the number at the end. Let’s try one: Number One from the Signed Number Lab assignment: Convert the decimal -68 into two’s complement notation. First thing to observe: we will wind up with an 8 bit number, and the first bit will be a 1. (Since the number is negative!) Step One: Convert as if it’s positive. So 6810 = ? In powers of two, I need a 64 and a 4 = 68… 26 = 64 1 25 = 32 0 24= 16 0 23 = 8 0 22 = 4 1 21 = 2 0 20 = 1 0 Step Two: Flip the bits 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 1 0 Step Three: Add One 0 1 1 1 0 1 0 1 1 1 1 0 Step Four: Throw in the first bit as a 1, since the number is negative. Final answer: 10111100 Make sense? This answer was perfect – 7 bits…what if the answer were smaller than 7 bits? Just add LEADING zeroes to get up to the first bit, which ALWAYS reflects the sign. What if the number we wanted to convert from decimal, base ten into two’s complement was positive 68 instead of negative 68? Just convert the number like we did above in Step One, and then slam a 0 in the first bit to indicate a positive number: Step One: Convert to base ten 26 = 64 25 = 32 24= 16 1 0 0 23 = 8 0 22 = 4 1 21 = 2 0 20 = 1 0 Step Two: Place a 0 in the front most bit. So, final answer: 01000100 Okay – we see how to take both a negative and a positive base ten number and convert them to two’s complement. Let’s go the other way: a two’s complement number converted back to decimal, base ten format. As an example, we will do problem 9 from the Signed Conversion Lab assignment: Convert the two’s complement number 11111111 into decimal notation. Step One: Observe the first bit to determine whether the number is positive or negative. Since the bit is a 1, we know the number is negative. Set the first bit aside, leaving the remaining 7 bits: 1111111 Since the complemented number was negative, we know that the number had it’s bits flipped, and a 1 added. So, we reverse that. We will need to subtract a 1, and then flip the bits. Step Two: Subtract One 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 0 0 1 1 1 Step Three: Flip the Bits 1 0 1 0 1 0 Step Four:Convert from a base 2 to base 10 number. This one is pretty easy – 1 in base two is still one in base 10! Throw the negative sign back in, and our final answer: -1! Let’s do one more problem… we’ll up the ante and do problem 5 from the Signed Conversion Sheet: Convert two’s complement values 11001000 and 00100101 to base ten integers, and add them. What is the result ,in two’s complement? Start off looking at the 1st number to be added: 11001000, in two’s complement. Observe the first bit. It’s a 1, so we know the number is negative, and it was complemented with flipped bits and an added one. So, reverse the complementation by subtracting one, then flipping the bits. Next, translate to base ten Finally, throw the negative sign back on. Here we go: Step One: Set the first bit aside, and subtract one from the remaining 7 bits: 1 0 0 1 0 0 0 1 1 0 0 0 1 1 1 Step Two: Flip the Bits 1 0 0 0 1 1 0 1 1 0 1 0 Step Three: Convert this base two number (111000) into base ten: 26 = 64 25 = 32 24= 16 23 = 8 22 = 4 21 = 2 0 1 1 1 0 0 1 0 20 = 1 0 So, we have 32 + 16 + 8 = 56. Throw on the negative sign: -56, FINAL ANSWER Arrgh… who makes up these problems? Now we have to do the same thing with the second number to be added: 00100101 Finally, our first break! We observe the first bit and see that the number is positive. (Since the leading bit was a 0). “All” we have to do here is peel off the leading zero, and convert the remaining 7 digits to a base ten number: Convert this base two number (111000) into base ten: 26 = 64 25 = 32 24= 16 23 = 8 22 = 4 0 1 0 0 1 21 = 2 0 20 = 1 1 So, we have 32 + 4 + 1 = 37. This is a positive number, so FINAL ANSWER. Next, we add the converted numbers: -56 + 37 = -19 If we can just convert decimal -19 into two’s complement, we are done! We need to produce an 8 digit number, the leading bit of which will be the sign. In this case, since the number is -19, we know that the leading bit will be a 1. Let’s set the first bit aside for a moment, and convert 19…. Convert this base ten number to base two… For 19, I need a 16, a 2 and a 1: 26 = 64 0 25 = 32 0 24= 16 1 23 = 8 0 22 = 4 0 21 = 2 1 20 = 1 1 Step Two: Flip the Bits 0 0 1 1 1 0 0 1 0 1 1 0 1 0 Step Three: Add 1 1 1 0 1 1 0 1 0 1 1 0 0 1 1 Now, flip the bits 1 Step Four: Add the leading sign bit to show negative 11101101 - Final Answer! There, now you have toured Two’s Complement – wasn’t that bad, was it? Hope this helped!