Ah, two's complement! Sounds like something you'd order at a fancy digital restaurant. But no, it's just how computers pull a fast one when dealing with those pesky negative numbers. Apparently, the old sign and magnitude wasn't quite sneaky enough for them.
Now, if you've got a positive denary number (that's a normal number to you and me), converting it to two's complement is surprisingly… well, normal. You just bung it into binary as you usually would. But here's the little quirk: to really show it's a goody-two-shoes positive number, the very first bit on the left (the most important one, the big cheese) must be a 0. Think of it as a digital halo.
Take the example: 30 in binary is 11110. But hold your horses! That starts with a 1, which in the world of two's complement could mean trouble (i.e., a negative number). So, to make it clear it's a happy, positive 30, you slap a 0 at the front: 011110. See? All smiles now.
Right, this is where things get a bit more… interesting. If you've got a negative denary number and you want to turn it into two's complement, there's a little dance you have to do. We'll stick to one method here to save our sanity.
First, you take the denary number (ignore the minus sign for a moment) and convert it into plain old binary. Easy enough.
Then comes the fun bit: you have to "flip every bit". That means all the 1s become 0s, and all the 0s become 1s. It's like the binary numbers have suddenly decided to swap identities.
Finally, you add binary 1 to the result of that flipping frenzy. Yes, just a simple 1. It's this little addition that makes the whole two's complement system work its magic (or its dark arts, depending on how you look at it).
So, there you have it. Positive numbers are just themselves with a polite 0 at the front. Negative numbers go through a digital obstacle course of flipping and adding. Don't say computers don't have a sense of drama!
Convert the denary number -45 into two's complement (using 8 bits).
Step 1: write +45 in binary (add 0s at the left to show it is positive and fill 8 bits).
00101101
Step 2: is the number you are converting negative? Yes, so flip every bit.
11010010
Step 3: add 1.
| 1 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | |
| + | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | |
Step 4: write the answer.
11010011
Step 1: write 70 in binary.
01000110
Step 2: is the number you are converting negative? No, so double check there is a 0 at the front.
Step 3: write the answer.
01000110
Step 1: write +103 in binary.
01100111
Step 2: is the number you are converting negative? Yes, so flip every bit.
Step 3: add 1.
Step 4: write the answer.
10011001
Step 1: write +210 in binary.
Step 2: is the number you are converting negative? Yes, so flip every bit.
Step 3: add 1.
Step 4: write the answer.
100101110
Answers
So, the sneaky computers have given you a number in their twisted two's complement form, and now they expect you to figure out what normal number it actually represents. It's like they're speaking in code and you've forgotten the secret handshake. Fear not, intrepid explorer of the digital wilderness! We can crack this.
First things first, you need to play detective and figure out if the two's complement number is a good egg (positive) or a right rotten one (negative). The big clue is that very first bit on the left. It's like the digital equivalent of a sneer or a smile.
If that first bit is a big fat 1, then BAM! You've got a negative number on your hands. It's wearing a digital disguise. But if that first bit is a nice, innocent 0, then you're in positive territory. Phew!
If you've spotted a 0 at the beginning, congratulations! You've got a positive two's complement number. Converting it back to denary is as simple as converting any ordinary binary number. No sneaky tricks here. For example, if you see 0100110, you just treat it like regular binary, and it happily turns into the denary number 38. Easy peasy.
Now, if that first bit was a 1, buckle up, buttercup! We need to undo the computer's little game. To get back to the real negative denary number, we have to reverse the "flipping" we talked about earlier. Here's the easiest way to do this digital U-turn:
Starting from the right-hand side of the two's complement number, you write down all the bits exactly as they are, up to and including the very first 1 you encounter. Stop there!
Then, for all the remaining bits to the left of that first 1, you do the opposite – you flip them! So, all the 1s become 0s, and all the 0s become 1s.
Finally, once you've done all that flipping, you treat the resulting binary number as a positive value and work out its denary equivalent. And don't forget! Since we started with a 1 at the beginning of the two's complement number, the denary number you've just calculated is actually negative. So, slap a minus sign on the front and Bob's your uncle!
For example, let's take the two's complement number 11010110. Starting from the right, we go 0, 1 (stop here!). Now, we flip the remaining bits to the left: 1101 becomes 0010. So we have 001010. Converting that to denary gives us 10. Since the original number started with a 1, the answer is -10. Clever, eh? In a slightly devious, computer-y way.
Convert the two's complement number 1110 into denary (using 4 bits).
Step 1: is the number you are converting negative? Yes (because the leftmost bit is 1).
Step 2: from right to left, write all the bits up to and including the first 1 and flip the rest.
| Before | 1 | 1 | 1 | 0 |
|---|---|---|---|---|
| After | 0 | 0 | 1 | 0 |
Step 3: treat it as a positive binary number and convert it to denary, 0010 = 2.
Step 4: write the answer with a '-', -2.
Convert the two's complement number 101101 into denary (using 6 bits).
Step 1: is the number you are converting negative? Yes (because the leftmost bit is 1).
Step 2: from right to left, write all the bits up to and including the first 1 and flip the rest.
| Before | 1 | 0 | 1 | 1 | 0 | 1 |
|---|---|---|---|---|---|---|
| After | 0 | 1 | 0 | 0 | 1 | 1 |
Step 3: treat it as a positive binary number and convert it to denary, 010011 = 19.
Step 4: write the answer with a '-', -19.
Convert the two's complement number 011001 into denary (using 6 bits).
Step 1: is the number you are converting negative? No (because the leftmost bit is 0).
Step 2: convert it as a positive binary number, 011001 = 25.
Step 1: Is the number you are converting negative? No
Step 2: Convert it as a positive binary number.
Step 3: Write the answer.
Answer: 011012 = 1410
Step 1: Is the number you are converting negative? Yes
Step 2: From right to left, write all the bits up to and including the first 1 and flip the rest.
Step 3: Treat it as a positive binary number and convert it to denary.
Step 4: Write the answer with a '-'.
Answer: 0110102 = 2610 then -2610
Step 1: Is the number you are converting negative? Yes
Step 2: From right to left, write all the bits up to and including the first 1 and flip the rest.
Step 3: Treat it as a positive binary number and convert it to denary.
Step 4: Write the answer with a '-'.
Answer: 000100102 = 1810 then -1810
Answers
So, you've got your unsigned binary numbers, all happy-go-lucky and starting from zero upwards. They're like a digital queue where everyone's in a positive mood. But then along comes two's complement, and things get a bit… partitioned. It turns out, the range of numbers you can squeeze into the same number of bits isn't quite the same. It's like having a party where some guests are only allowed in the 'positive zone' and others are stuck in the 'negative corner'.
Take, for instance, a humble 8-bit number. If it's unsigned, it can cheerfully count all the way from a big fat 0 up to a respectable 255. Plenty of room for digital shenanigans!
But if you're using two's complement with those same 8 bits, things get a bit… restricted. You can only represent numbers from a rather chilly -128 up to a slightly less chilly +127. Notice how the positive end isn't quite as high as the unsigned version? It's because that first bit is doing double duty as the sign, hogging some of the potential for bigger positive numbers. Sneaky, those computers!
Now, if you want to work out the top end of the positive range in two's complement, there's a bit of a formula you can use: 2n-1 - 1, where n is the number of bits you're dealing with.
Let's look at their example: 6 bits. Plug that into our formula: 26-1 - 1 = 25 - 1 = 32 - 1 = 31. So, with 6 bits in two's complement, the biggest positive number you can represent is 31.
Notice that the formula only gives you the top of the positive range. Working out the bottom end (the most negative number) is slightly different – for n bits, it's usually -2n-1. So, for our 6-bit example, the most negative number would be -26-1 = -25 = -32.
So, the key takeaway here is that two's complement lets you represent negative numbers, which is jolly useful, but it comes at the cost of a slightly smaller positive range compared to its unsigned cousin. It's all a matter of digital give and take, isn't it?
What is the range of numbers that can be stored in two's complement with 5 bits?
The range of numbers in two's complement with n bits is from -(2n-1) to +(2n-1 - 1).
For 5 bits (n=5):
Smallest value: -(25-1) = -(24) = -16
Largest value: +(25-1 - 1) = +(24 - 1) = + (16 - 1) = +15
The range is -16 to +15.
Copy the line below and fill in the gaps. Remember that n is the number of bits (11).
-2n-1 = -211-1 = -210 = -1024
2n-1 - 1 = 211-1 - 1 = 210 - 1 = 1024 - 1 = 1023
The range is from -1024 to 1023.
Copy the line below and fill in the gaps.
-2n-1 = -213-1 = -212 = -4096
2n-1 - 1 = 213-1 - 1 = 212 - 1 = 4096 - 1 = 4095
The range is from -4096 to 4095.
Answers
Now, hold on to your hats, because this is where things get a tad… upside down. It turns out that computers, bless their simple digital hearts, aren't all that keen on subtraction. It's a bit too much bother for them. So, what do they do instead? They cheat! They use the wonderful weirdness of two's complement numbers to turn subtraction into… drum roll please… addition!
Think about it in the normal world for a moment. Saying you want to calculate 33 - 10 is exactly the same as saying you want to calculate 33 + (-10). See? Subtraction is just adding a negative number in disguise!
And that's precisely what two's complement lets us do in the binary world. If you want to subtract one binary number from another, the computer can take the number you want to subtract, turn it into its negative two's complement form (we talked about how to do that earlier, remember?), and then simply add the two numbers together.
So, instead of faffing about with borrowing and all that subtraction nonsense, the computer just does what it's good at: adding binary numbers. If you've forgotten how that works, you might want to go back and have a peek at the section on addition. It all ties together, you see! This clever little trick is why two's complement is so darn useful in the inner workings of your computer. It makes things much simpler (for the computer, at least!).
Perform the binary subtraction of 12 - 4.
Step 1: turn it into an addition, 12 + (-4).
Step 2: turn the first number into binary (make sure it is positive with a 0 at the front). 12 = 01100
Step 3: turn the second number into two's complement (using 5 bits to match the length of 12 in binary with a leading zero).
+4 = 00100
-4 = 11011 (flip bits) + 1 = 11100
Step 4: put the two numbers in a table ready to add together. Make sure they both have the same number of bits.
| +12 | 0 | 1 | 1 | 0 | 0 |
| -4 | 1 | 1 | 1 | 0 | 0 |
| Result | 0 | 1 | 0 | 0 | 0 |
| Carry | 1 | 1 | 1 | ||
The answer is 01000.
Perform the binary subtraction of 59 - 20.
Step 1: turn it into an addition, 59 + (-20).
Step 2: turn the first number into binary (make sure it is positive with a 0 at the front). 59 = 0111011
Step 3: turn the second number into two's complement (using 7 bits to match the length of 59 in binary with a leading zero).
+20 = 0010100
-20 = 1101011 (flip bits) + 1 = 1101100
Step 4: put the two numbers in a table and add them. (Fill in any gaps so they are the same length.)
| +59 | 0 | 1 | 1 | 1 | 0 | 1 | 1 |
| -20 | 1 | 1 | 0 | 1 | 1 | 0 | 0 |
| Result | 0 | 1 | 0 | 0 | 1 | 1 | 1 |
| Carry | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
The answer is 0100111.
Step 1: Turn it into addition. 40 + -5
Step 2: Turn the first number (40) into binary.
4010 = 001010002
Step 3: Turn the second number (-5) into two's complement.
510 = 000001012
Flip the bits: 11111010
Add 1: 11111011
-5 in two's complement = 11111011
Step 4: Put the numbers in a table and add them.
Answer: 00100011
Step 1: Turn it into addition. 88 + -22
Step 2: Turn the first number (88) into binary.
8810 = 010110002
Step 3: Turn the second number (-22) into two's complement.
2210 = 000101102
Flip the bits: 11101001
Add 1: 11101010
-22 in two's complement = 11101010
Step 4: Put the numbers in a table and add them.
Answer: 01000010
Answers