Two's Complement: The Computer's Clever Little Lie

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.

Denary to Two's Complement: The Positive Pretenders

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.

Denary to Two's Complement: The Negative Nasties

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!

Worked Examples Icon Worked examples

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.

11010010
+00000001
11010011

Step 4: write the answer.

11010011

Guide Icon Guided Practice

1. Convert the denary number 70 into two's complement.

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

2. Convert the denary number -103 into two's complement.

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

3. Convert the denary number -210 into two's complement.

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

Task List Icon Independent Practice

  1. Convert the decimal number +35 into two's complement (8-bit).
  2. Convert the decimal number +150 into two's complement (8-bit).
  3. Convert the decimal number -27 into two's complement (8-bit).
  4. Convert the decimal number -85 into two's complement (8-bit).
  5. Convert the decimal number +110 into two's complement (8-bit).
  6. Convert the decimal number -100 into two's complement (8-bit).
  7. Convert the decimal number -239 into two's complement (using a minimum number of bits).
  8. Convert the decimal number -302 into two's complement (using a minimum number of bits).
  9. Convert the decimal number +321 into two's complement (using a minimum number of bits).
  10. Convert the decimal number -400 into two's complement (using a minimum number of bits).
  11. Convert the decimal number +185 into two's complement (using a minimum number of bits).
  12. Convert the decimal number -513 into two's complement (using a minimum number of bits).

Answers

  1. 00100011
  2. 10010110
  3. 11100101
  4. 10101011
  5. 01101110
  6. 10011100
  7. 100010001 (9 bits)
  8. 1011010010 (10 bits)
  9. 101000001 (9 bits)
  10. 1001110000 (10 bits)
  11. 010111001 (9 bits)
  12. 110111111111 (12 bits)

Two's Complement to Denary: Unmasking the Digital Deception

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.

Spotting the Good Eggs and the Rotten Ones

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!

Positive Numbers: As Easy as Digital Pie

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.

Negative Numbers: Reversing the Digital Shenanigans

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.

Worked Examples Icon Worked examples

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.

Before1110
After0010

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.

Before101101
After010011

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.

Guide Icon Guided Practice

1. Convert the two's complement number 01101 into denary.

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

2. Convert the two's complement number 100110 into denary.

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

3. Convert the two's complement number 11101110 into denary.

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

Task List Icon Independent Practice

  1. Convert the two's complement number 01101 to denary.
  2. Convert the two's complement number 110011 to denary.
  3. Convert the two's complement number 1011001 to denary.
  4. Convert the two's complement number 0101101 to denary.
  5. Convert the two's complement number 011101101 to denary.
  6. Convert the two's complement number 111000110 to denary.
  7. Convert the two's complement number 101101010 to denary.
  8. Convert the two's complement number 010110001 to denary.
  9. Convert the two's complement number 0101110010101 to denary.
  10. Convert the two's complement number 1010011001010 to denary.

Answers

  1. +13
  2. -13
  3. -39
  4. +45
  5. +237
  6. -58
  7. -150
  8. +177
  9. +2965
  10. -1446

Range of Two's Complement Numbers: Not as Roomy as You Might Think!

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!

The Secret Formula for Positive Territory (Mostly)

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?

Worked Examples Icon Worked example

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.

Guide Icon Guided Practice

1. What is the range of numbers that can be stored in two's complement with 11 bits?

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.

2. What is the range of numbers that can be stored in two's complement with 13 bits?

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.

Task List Icon Independent Practice

  1. What is the range of numbers that can be stored in two's complement with 3 bits?
  2. What is the range of numbers that can be stored in two's complement with 4 bits?
  3. What is the range of numbers that can be stored in two's complement with 10 bits?
  4. What is the range of numbers that can be stored in two's complement with 12 bits?
  5. What is the range of numbers that can be stored in two's complement with 16 bits?

Answers

  1. -4 to +3
  2. -8 to +7
  3. -512 to +511
  4. -2048 to +2047
  5. -32768 to +32767

Subtracting with Two's Complement: Adding is the New Subtracting!

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!).

Worked Examples Icon Worked Example

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.

+1201100
-411100
Result01000
Carry111

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.)

+590111011
-201101100
Result0100111
Carry1111111

The answer is 0100111.

Guide Icon Guided Practice

1. Perform the binary subtraction 40 - 5.

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

2. Perform the binary subtraction 88 - 22.

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

Task List Icon Binary Subtraction Practice

  1. Perform the binary subtraction 12 - 5.
  2. Perform the binary subtraction 30 - 7.
  3. Perform the binary subtraction 65 - 11.
  4. Perform the binary subtraction 100 - 3.
  5. Perform the binary subtraction 150 - 150.
  6. Perform the binary subtraction 180 - 55.
  7. Perform the binary subtraction 210 - 205.
  8. Perform the binary subtraction 240 - 31.
  9. Perform the binary subtraction 190 - 125.
  10. Perform the binary subtraction 300 - 250.

Answers

  1. 1100 - 0101 = 0111 (Denary: 7)
  2. 11110 - 00111 = 10111 (Denary: 23)
  3. 1000001 - 0001011 = 0110110 (Denary: 54)
  4. 1100100 - 0000011 = 1100001 (Denary: 97)
  5. 10010110 - 10010110 = 00000000 (Denary: 0)
  6. 10110100 - 00110111 = 01111101 (Denary: 125)
  7. 11010010 - 11001101 = 00000101 (Denary: 5)
  8. 11110000 - 00011111 = 11010001 (Denary: 209)
  9. 10111110 - 01111101 = 01000001 (Denary: 65)
  10. 100101100 - 011111010 = 0110010 (Denary: 50)