Two’s complement — practice with step-by-step guided problems: getting and undoing the two’s complement, ranges with n bits, subtractions in 4 and 8 bits and a chained addition in the exam format (and the carry I added out of habit)
In a class mock exam I had to subtract 75 − 30 in 8-bit two’s complement. I took the two’s complement of 30, added, got an end carry and, since I had just revised ones’ complement, I added it on the right “so it wouldn’t get lost”. Result: 46. The lecturer crossed it out with a single word in the margin: “C2”. I had spent the whole week with the end-around carry and my hands did it on their own. Since then, before every addition I write at the top which format I am in.
This practice is about that: doing two’s complement calculations without your hands going on autopilot. There are four exam-style problems solved step by step, with the reason for every step and the mistakes that usually cost points. We keep the 8-bit odometer from the theory and, in the last problem, we do a question in the exact format of the written exam.
Table of Contents
Before you start
It is worth being clear about:
- How to get the two’s complement of a number: flip and add 1, or copy from the right up to the first 1 and flip the rest.
- That the leftmost bit weighs (−128 in 8 bits) and that the range is … .
- That in two’s complement the end carry is discarded and overflow is read from the sign.
- Converting decimal, hexadecimal and octal to binary (from the number systems topic).
If any of them sounds shaky, review the theory before going on.
Keep squared paper at hand and write each bit in its own square. Half the mistakes in this topic are bits that slip one column.
How to solve a paper problem
The method is the same as in the previous topics, in four steps:
- Data: how many bits, which format, which base each number comes in. Write it at the top (“C2, 8 bits”).
- Method: what you are going to do and in what order (convert to binary → take the two’s complement of the negatives → add → look at the signs).
- Solve showing every step: the two’s complement of each number, the column addition with the row of carries and the result in decimal.
- Check: convert the result to decimal and compare it with the decimal sum. If it matches (or if you know why it doesn’t, because there is overflow), you are done.
Problem 1: getting and undoing the two’s complement
Statement. Working in 8-bit two’s complement:
a) Represent −53 and −77 with both methods (flip and add 1, and the shortcut). b) Give the decimal value of 10110110, 01110110 and 11111000. c) A register holds E5₁₆ in 8-bit two’s complement. Which number is it?
The idea
In a) we go from decimal to two’s complement: first the positive with its 8 bits and then the two’s complement. In b) and c) we go the other way, and the first thing is to look at the leftmost bit: if it is 0, it is read as usual; if it is 1, it is negative and is read with the weight −128 (or by undoing the two’s complement).
Before calculating, write down the sign you expect. A code that starts with 1 must end up as a negative number; if you get a positive, you have done something wrong.
Step by step
a) −53. 53 = 32 + 16 + 4 + 1, so +53 = 00110101.
| Step | Bits | What was done |
|---|---|---|
| Positive | 00110101 | 53 in 8 bits, with two zeros in front |
| Ones’ c. | 11001010 | The 8 bits are flipped |
| Ones’ c. + 1 | 11001011 | The last bit was 0: the +1 causes no carries |
With the shortcut, the first 1 from the right is the last bit: that 1 is copied and the other seven are flipped (0011010 → 1100101). You get 11001011 again. With odd numbers the shortcut is almost a ones’ complement: only the last bit is copied.
a) −77. 77 = 64 + 8 + 4 + 1 → 01001101. Flipped, 10110010; plus 1, 10110011. With the shortcut: copy the last 1 and flip 0100110 → 1011001: 10110011. They match.
b) Decimal values. With the weight −128 on the first bit:
| Code | First bit | Sum | Value |
|---|---|---|---|
| 10110110 | 1 → negative | −128 + 32 + 16 + 4 + 2 | −74 |
| 01110110 | 0 → positive | 64 + 32 + 16 + 4 + 2 | +118 |
| 11111000 | 1 → negative | −128 + 64 + 32 + 16 + 8 | −8 |
To check the first one the other way: the two’s complement of 10110110 is 01001010 = 64 + 8 + 2 = 74, so the number is −74.
The third code, 11111000, is good for seeing the shortcut with an even number, which is where people usually slip. You look for the first 1 starting from the right, copy up to it (included) and flip everything left of it:
| Step | Bits | Why |
|---|---|---|
| Code | 11111000 | It starts with 1: it is negative, so the two’s complement has to be undone |
| Copied | …1000 | The three 0s on the right and the first 1 go through unchanged |
| Flipped | 0000…. | The four 1s on the left become 0 |
| Result | 00001000 | 8, so the number was −8 |
The other way gives the same: flipped, 00000111; plus 1, 00001000. Here the +1 does ripple carries (7 + 1 = 8), and the shortcut saves you them.
Copying only the last bit with even numbers too. With 11111000 you would get 00000110 = 6, and the number would look like −6. The shortcut copies up to the first 1, not just the last bit: with odd numbers they are the same thing, with even numbers they are not.
c) E5₁₆. Each hex digit is 4 bits: E = 1110 and 5 = 0101, so the code is 11100101. It starts with 1: negative. With weights, −128 + 64 + 32 + 4 + 1 = −27. Undoing the two’s complement: 00011011 = 16 + 8 + 2 + 1 = 27. The number is −27, not 229.
What would happen if the register in part c) had 16 bits?
What changes is how many bits the code has. E5₁₆ in 16 bits is written 00E5₁₆, that is, 0000000011100101: the same 8 bits as before with eight zeros in front. Now the leftmost bit is a 0, so it is positive and is read as unsigned binary: 128 + 64 + 32 + 4 + 1 = 229.
The same bit pattern, 11100101, is −27 in 8 bits because its first bit is the sign bit; in 16 bits that 1 is no longer in the sign column and weighs +128. To store −27 in 16 bits you would need eight ones in front: 1111111111100101, which in hexadecimal is FFE5₁₆.
That is why the question always says how many bits the register has: without that piece of information, E5₁₆ has no single value.
Check
A negative and its positive must add up to 256 if you read them as unsigned: 11001011 = 203 and 203 + 53 = 256; 10110011 = 179 and 179 + 77 = 256. It is the formula from the theory.
Questions you might ask
Why do I have to write the leading zeros before flipping? Because when you flip them those zeros become the ones of the sign. If you flip 110101 (six bits) you get 001010, which with two zeros in front would be positive: you have lost the sign.
Can I take the two’s complement straight from the hexadecimal? Not safely. Convert to binary with its 8 bits first and work there.
What if I am given the number in two’s complement and it is positive? Nothing is done: 01110110 is read as plain binary. The two’s complement is only “undone” for negatives.
Which method should I use in the exam? Whichever is faster for you, but check with the other one if you have time left. It takes thirty seconds and it is the typical one-bit mistake.
Answering 229 in part c). E5₁₆ “is” 229 in unsigned binary, but the question says 8-bit two’s complement, and 229 does not even fit in −128 … +127.
Try it yourself
- Represent −99 and −1 in 8-bit two’s complement with both methods.
- Which number is 10000001? And B0₁₆, in 8-bit two’s complement?
Problem 2: ranges with n bits and the four formats side by side
Statement.
a) What range of values can be represented in two’s complement with 6, 10 and 12 bits? b) What value does the 6-bit code 101101 have in unsigned binary, sign-magnitude, ones’ complement and two’s complement? c) What is the minimum number of bits needed to represent −70, +70, −64 and +64 in two’s complement?
The idea
The range comes from the formula . In b) the same code is read with four different rules, so each one gives a different number. In c) you have to find the smallest n whose range contains the number, and beware of the asymmetry: the negative fits “a little better” than the positive.
For c), learn the powers of 2: 32, 64, 128. With n bits the minimum is and the maximum, one less than .
Step by step
a) Ranges.
| n | ||
|---|---|---|
| 6 | −32 | +31 |
| 10 | −512 | +511 |
| 12 | −2048 | +2047 |
Where the first row comes from. With 6 bits there are 64 different codes. Half of them, 32, start with 1 and are the negatives: from −1 to −32. The other half start with 0, but one of them is zero, so only 31 positives are left. With 10 and 12 bits the same thing happens: the negative side wins by one because zero stays on the side of the codes that start with 0.
b) 101101 in four ways. The 6-bit weights are 32, 16, 8, 4, 2 and 1.
| Format | How it is read | Value |
|---|---|---|
| Unsigned binary | 32 + 8 + 4 + 1 | 45 |
| Sign-magnitude | Sign 1 (negative), magnitude 01101 = 13 | −13 |
| Ones’ complement | Flip it: 010010 = 18 | −18 |
| Two’s complement | −32 + 8 + 4 + 1 (or two’s c.: 010011 = 19) | −19 |
Note the relationship between ones’ and two’s complement: the same negative code is worth one less in two’s complement (−19) than in ones’ complement (−18). It is the +1 we add after flipping.
c) Minimum bits.
| Number | Fits in 7 bits (−64 … +63)? | And in 8 (−128 … +127)? | Minimum | Code |
|---|---|---|---|---|
| −70 | No | Yes | 8 | 10111010 |
| +70 | No | Yes | 8 | 01000110 |
| −64 | Yes | Yes | 7 | 1000000 |
| +64 | No (the maximum is +63) | Yes | 8 | 01000000 |
−64 fits in 7 bits and +64 does not: the asymmetry of the range in its purest form.
Check
The 7-bit code 1000000 with the negative weight is . And in b), two’s c. = ones’ c. − 1 for negatives: −18 − 1 = −19. It adds up.
Questions you might ask
Why is nothing flipped in sign-magnitude? Because the magnitude is stored as it is; only the first bit gives the sign. Flipping belongs to ones’ and two’s complement.
Are positives read the same in all four formats? Yes: if the code starts with 0, unsigned binary, SM, ones’ and two’s complement give the same number.
Why is the maximum +63 with 7 bits and not +64? Because 64 codes start with 0 and one of them is zero: 63 positives are left.
Saying −64 needs 8 bits “because 64 needs 7 plus the sign”. In two’s complement the smallest 7-bit negative is precisely −64.
Try it yourself
- Read the 6-bit code 110011 in the four formats.
- What is the minimum number of bits for −200 in two’s complement? And for +200?
Problem 3: subtractions in 4 and 8 bits, with carries and overflows
Statement. Do these subtractions by adding the two’s complement of the subtrahend, and say for each one whether there is an end carry and whether there is overflow.
- In 4 bits: a) 3 − 6; b) 5 − (−4); c) −2 − 5.
- In 8 bits: d) 75 − 30; e) −90 − 50.
The idea
Each subtraction becomes an addition: . Subtracting a negative is adding its opposite (5 − (−4) = 5 + 4). Then, two separate questions: does a carry come out on the left? (it is thrown away) and is the sign of the result the one it should be? (if not, overflow).
Write the operation already turned into an addition before converting to binary: “3 + (−6)”, “5 + 4”, “−2 + (−5)”. That way you know which two signs you are adding.
Step by step
a) 3 − 6 = 3 + (−6). In 4 bits, 3 = 0011 and 6 = 0110, whose two’s complement is 1010.
carries . 1 . .
0 0 1 1 +3
+ 1 0 1 0 −6
---------
1 1 0 1 −3
No end carry. Different signs, so there cannot be overflow. 1101 = −8 + 4 + 1 = −3.
b) 5 − (−4) = 5 + 4. 5 = 0101 and 4 = 0100.
carries 1 . . .
0 1 0 1 +5
+ 0 1 0 0 +4
---------
1 0 0 1 −7 (overflow)
Two positives have given 1001, which starts with 1: overflow. The real sum is 9, and in 4 bits the maximum is +7. On the 4-bit wheel you can see it perfectly: from +5 you move 4 positions forward, cross the red mark and land on −7.
c) −2 − 5 = −2 + (−5). −2 = 1110 and −5 = 1011.
carries 1 1 . .
1 1 1 0 −2
+ 1 0 1 1 −5
---------
[1] 1 0 0 1 −7
^ end carry: discarded
End carry, which is thrown away. Two negatives have given 1001 (−7), negative: no overflow. −2 − 5 = −7, correct.
d) 75 − 30 = 75 + (−30). 75 = 01001011; 30 = 00011110, two’s c. = 11100010.
carries 1 . . . . 1 . .
0 1 0 0 1 0 1 1 +75
+ 1 1 1 0 0 0 1 0 −30
-----------------
[1] 0 0 1 0 1 1 0 1 +45
^ end carry: discarded
Here is my story: an end carry comes out and is discarded. The result is 00101101 = +45, exactly 75 − 30. If you add it on the right as in ones’ complement, you get 46.
The addition in d), column by column. Columns are numbered from the right, starting at 0, and in each one you add the two bits and the carry coming in from the previous one:
| Column | 75 | −30 | Carry in | Sum | Written | Carry out | Why |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 + 0 = 1 |
| 1 | 1 | 1 | 0 | 2 | 0 | 1 | 1 + 1 = 10 in binary: write 0 and carry 1 |
| 2 | 0 | 0 | 1 | 1 | 1 | 0 | Only the carry adds up |
| 3 | 1 | 0 | 0 | 1 | 1 | 0 | 1 + 0 = 1 |
| 4 | 0 | 0 | 0 | 0 | 0 | 0 | Nothing to add |
| 5 | 0 | 1 | 0 | 1 | 1 | 0 | 0 + 1 = 1 |
| 6 | 1 | 1 | 0 | 2 | 0 | 1 | 1 + 1 again: 0 and carry 1 |
| 7 | 0 | 1 | 1 | 2 | 0 | 1 | Sign column: 0 + 1 + 1 = 10; the 1 that comes out is the end carry |
Read from column 7 down to column 0, the written bits give 00101101. The carry coming out of column 7 has no room in 8 bits: it is the one you throw away.
What would happen if you added the end carry on the right, as I did in the mock exam?
You get 00101101 + 1 = 00101110, which is 46: one more than it should be. To see why, read the addition as if they were unsigned numbers. The two’s complement of 30, 11100010, read as unsigned binary is 226, which is 256 − 30. So what you have really added is 75 + 226 = 301 = 256 + 45.
That extra 256 is exactly the end carry: a 1 in column 8, which is worth 256. If you throw it away, you remove the 256 and 45 is left. If you also add it on the right, you remove 256 but put back 1, and you end up with 46.
In ones’ complement the sum is different: the ones’ complement of 30 is 255 − 30, one less than in two’s complement. That is why there you do have to give that 1 back on the right. In two’s complement you already added the +1 when you built the complement; if you add it again, you count it twice.
e) −90 − 50 = −90 + (−50). −90 = 10100110 and −50 = 11001110.
carries . . . 1 1 1 . .
1 0 1 0 0 1 1 0 −90
+ 1 1 0 0 1 1 1 0 −50
-----------------
[1] 0 1 1 1 0 1 0 0 +116 (overflow)
^ end carry: discarded
Two negatives have given 01110100, which starts with 0 (+116): overflow. −140 does not fit in −128 … +127. There is also an end carry, but that has nothing to do with it.
| Subtraction | Result | End carry | Overflow |
|---|---|---|---|
| 3 − 6 | 1101 = −3 | No | No |
| 5 − (−4) | 1001 → −7 | No | Yes |
| −2 − 5 | 1001 = −7 | Yes | No |
| 75 − 30 | 00101101 = +45 | Yes | No |
| −90 − 50 | 01110100 → +116 | Yes | Yes |
Check
The three results without overflow match the decimal sums (−3, −7, 45). In the two with overflow, the number you get is shifted by exactly from the real sum: 9 − 16 = −7 in 4 bits and −140 + 256 = 116 in 8 bits. It is the wheel wrapping round.
Questions you might ask
Why is there no carry in a) and there is one in d), if both are “positive minus positive”? Because the carry depends on the bits, not on whether the sum is right. In a) the result is negative and in d) positive; when adding a positive and a negative gives a positive result, a carry always comes out.
And the rule of the carry going into and out of the sign? In b) a 1 goes into the sign column and nothing comes out: different, overflow. In e) a 0 goes in and a 1 comes out: different. In c) and d) a 1 goes in and a 1 comes out: the same, all fine.
Do I have to give the result even if there is overflow? Yes: the exam asks for the code you get (01110100) and, separately, that you say there is overflow. Do not write −140 in binary, because it does not exist in 8 bits.
Saying that c) and d) overflow because an end carry comes out. In two’s complement the carry is thrown away and warns of nothing; only the sign tells you about overflow.
Try it yourself
- In 4 bits: 6 − (−2) and −7 − 1. Which one overflows?
- In 8 bits: 100 − (−28) and −128 − 1.
Problem 4: a question in the exam format
The first question of the theory exam is usually a chained addition of three numbers in different bases, in 8-bit two’s complement, with two boxes: the result and whether there is overflow. Here is one with other numbers:
Question (1 point). Given the chained addition , convert the numbers to 8-bit two’s complement binary, do the chained addition and state whether overflow occurs. Result of the chained addition in 8-bit two’s complement: ________ · Does overflow occur? Yes / No
The idea
Three phases, in order: convert the three numbers to 8-bit binary (and take the two’s complement of the two negatives), add first what is inside the brackets and then the third number, and decide whether there is overflow by looking at the sign of each addition.
The chained addition is two additions. Check for overflow in both, not only in the last one.
Step by step
1. Convert.
| Number | Positive in binary | Two’s c. (if negative) | Decimal |
|---|---|---|---|
| 58 = 32 + 16 + 8 + 2 → 00111010 | 11000110 | −58 | |
| 3 = 0011, D = 1101 → 00111101 | 11000011 | −61 | |
| 2 = 010, 7 = 111 → 00010111 | (positive) 00010111 | +23 |
For 58, successive divisions (58/2 = 29 remainder 0, 29/2 = 14 remainder 1, 14/2 = 7 remainder 0, 7/2 = 3 remainder 1, 3/2 = 1 remainder 1, and the last quotient 1) give 111010, which with two zeros in front is 00111010. For hexadecimal and octal, each digit is a group of 4 or 3 bits.
The two two’s complements in the table, with the shortcut:
| Number | Positive | Copied (up to the first 1 from the right) | Flipped | Two’s complement | Why |
|---|---|---|---|---|---|
| −58 | 00111010 | 10 | 001110 → 110001 | 11000110 | 58 is even: the first 1 is in the second column, so two bits are copied |
| −61 | 00111101 | 1 | 0011110 → 1100001 | 11000011 | 61 is odd: only the last bit is copied |
A quick check of each one: 11000110 read as unsigned binary is 198 and 198 + 58 = 256; 11000011 is 195 and 195 + 61 = 256.
2. First addition: (−58) + (−61).
carries 1 . . . 1 1 . .
1 1 0 0 0 1 1 0 −58
+ 1 1 0 0 0 0 1 1 −61
-----------------
[1] 1 0 0 0 1 0 0 1 −119
^ end carry: discarded
End carry, which is discarded. Two negatives have given 10001001, which starts with 1: negative, no overflow. With weights: −128 + 8 + 1 = −119, and −58 − 61 = −119.
3. Second addition: (−119) + 23.
carries . . 1 1 1 1 1 .
1 0 0 0 1 0 0 1 −119
+ 0 0 0 1 0 1 1 1 +23
-----------------
1 0 1 0 0 0 0 0 −96
No end carry. Different signs, so there cannot be overflow. The result is 10100000 = −128 + 32 = −96, and −119 + 23 = −96.
This addition has the longest chain of carries in the practice: column 0 (1 + 1) produces a carry and columns 1 to 4 keep passing it on, because in each of them a 1 from the operands meets the one coming in. In column 5 both bits are 0, the carry is written as a 1 and the chain stops. This is where a bit shifts most easily.
If a carry slips away in a chain like this, the result changes by a power of 2 and the sign may still look right. The check in decimal (−119 + 23 = −96) is what warns you.
4. Answer.
- Result of the chained addition in 8-bit two’s complement: 10100000 (−96).
- Does overflow occur? No.
Check and how it is marked
In decimal: −58 − 61 + 23 = −96, which is within −128 … +127, and both partial results fit too (−119 and −96). So there is no overflow in either addition.
An approximate breakdown of the point (it is mine, not official; every lecturer marks in their own way): about 0.3 for converting the three numbers correctly, 0.2 for each addition done right and 0.3 for the final result with the right answer about overflow. What costs the most: a two’s complement with one wrong bit (it drags the mistake into both additions), taking the end carry for overflow and not showing the working (without columns nothing can be given for the method).
Ticking “Yes” because the first addition produced an end carry. The carry is thrown away; two negatives that give a negative do not overflow.
Try it yourself
Do the same question with . A hint to check your answer: this time the first addition does overflow.
Common mistakes in this practice
- Adding the end carry on the right. That is ones’ complement; in two’s complement it is thrown away.
- Marking overflow because of the carry. Only the sign tells you about overflow: same sign in the addends and another in the result.
- Taking the two’s complement with fewer than 8 bits. First add the leading zeros; then flip.
- Reading a hexadecimal in two’s complement as unsigned. E5₁₆ in 8-bit two’s complement is −27, not 229.
- Forgetting that subtracting a negative is adding. 5 − (−4) is 5 + 4, not 5 + (−4).
- Checking overflow only in the last addition of a chained addition.
Check it with Python
All the calculations of this practice, done once with the same column-by-column adder from the theory:
# Problems of the two's complement practice: every calculation, once
def c1(b): # flip every bit
return ''.join('1' if x == '0' else '0' for x in b)
def c2(x, n=8): # x in n-bit C2
return format(x % 2 ** n, f'0{n}b')
def value(b): # the sign bit weighs -2^(n-1)
return -int(b[0]) * 2 ** (len(b) - 1) + int(b[1:], 2)
def add(a, b): # result, row of carries, end carry, overflow
carry, r, row = 0, '', ''
for i in range(len(a) - 1, -1, -1):
row = str(carry) + row
s = int(a[i]) + int(b[i]) + carry
r, carry = str(s % 2) + r, s // 2
return r, row, carry, a[0] == b[0] and r[0] != a[0]
def show(x, y, n=8): # prints an addition with everything an exam asks for
r, row, ac, d = add(c2(x, n), c2(y, n))
print(f' {x} + ({y}): carries {row} {c2(x, n)} + {c2(y, n)} = {r} = {value(r)} carry {ac} ovf. {d}')
return r
print('== Problem 1')
for x in (53, 77):
p = format(x, '08b')
print(f' +{x} = {p} C1 {c1(p)} C2 {c2(-x)} = {value(c2(-x))}')
for b in ('10110110', '01110110', '11111000', format(0xE5, '08b')):
print(f' {b} = {value(b)} (C2: {c2(-value(b))})')
print('== Problem 2')
for n in (6, 10, 12):
print(f' {n} bits: {-2 ** (n - 1)} .. {2 ** (n - 1) - 1}')
b = '101101'
print(f' {b}: unsigned {int(b, 2)}, SM {-int(b[1:], 2)}, C1 {value(b) + 1}, C2 {value(b)}')
for x in (-70, 70, -64, 64):
n = next(n for n in range(2, 12) if -2 ** (n - 1) <= x <= 2 ** (n - 1) - 1)
print(f' {x}: {n} bits ({c2(x, n)})')
print('== Problem 3')
for x, y in ((3, -6), (5, 4), (-2, -5)):
show(x, y, 4)
for x, y in ((75, -30), (-90, -50)):
show(x, y)
print('== Problem 4: ((-58(10 + (-3D(16)) + 27(8)')
print(f' -58 -> {c2(-58)}, 3D(16 = {0x3D} -> {c2(-0x3D)}, 27(8 = {0o27} -> {c2(0o27)}')
r = show(-58, -0x3D)
show(value(r), 0o27)
print('== Try it yourself: ((-90(10 + (-5A(16)) + 17(8)')
r = show(-90, -0x5A)
show(value(r), 0o17)
== Problem 1 +53 = 00110101 C1 11001010 C2 11001011 = -53 +77 = 01001101 C1 10110010 C2 10110011 = -77 10110110 = -74 (C2: 01001010) 01110110 = 118 (C2: 10001010) 11111000 = -8 (C2: 00001000) 11100101 = -27 (C2: 00011011) == Problem 2 6 bits: -32 .. 31 10 bits: -512 .. 511 12 bits: -2048 .. 2047 101101: unsigned 45, SM -13, C1 -18, C2 -19 -70: 8 bits (10111010) 70: 8 bits (01000110) -64: 7 bits (1000000) 64: 8 bits (01000000) == Problem 3 3 + (-6): carries 0100 0011 + 1010 = 1101 = -3 carry 0 ovf. False 5 + (4): carries 1000 0101 + 0100 = 1001 = -7 carry 0 ovf. True -2 + (-5): carries 1100 1110 + 1011 = 1001 = -7 carry 1 ovf. False 75 + (-30): carries 10000100 01001011 + 11100010 = 00101101 = 45 carry 1 ovf. False -90 + (-50): carries 00011100 10100110 + 11001110 = 01110100 = 116 carry 1 ovf. True == Problem 4: ((-58(10 + (-3D(16)) + 27(8) -58 -> 11000110, 3D(16 = 61 -> 11000011, 27(8 = 23 -> 00010111 -58 + (-61): carries 10001100 11000110 + 11000011 = 10001001 = -119 carry 1 ovf. False -119 + (23): carries 00111110 10001001 + 00010111 = 10100000 = -96 carry 0 ovf. False == Try it yourself: ((-90(10 + (-5A(16)) + 17(8) -90 + (-90): carries 01001100 10100110 + 10100110 = 01001100 = 76 carry 1 ovf. True 76 + (15): carries 00011000 01001100 + 00001111 = 01011011 = 91 carry 0 ovf. False
Note that the show function prints the same things the exam asks for: the row of carries, the two codes, the result, its value, the end carry and whether there is overflow. The last part solves the “Try it yourself” of problem 4: if you have done it by hand, compare here (and don’t look before).
Summary
- The two’s complement is obtained by flipping and adding 1, or by copying from the right up to the first 1 and flipping the rest; it is undone the same way.
- A code that starts with 1 is negative: it is read with the weight −128 (in 8 bits) or by undoing the two’s complement.
- The range with n bits is … : −64 fits in 7 bits and +64 does not.
- Subtracting is adding the two’s complement of the subtrahend; subtracting a negative is adding its opposite.
- The end carry is thrown away; overflow is detected by the sign (or by the carry going into and out of the sign column).
- In a chained addition, overflow is checked in every addition.
Checklist: have you practised it?
- Can you take the two’s complement of a number with both methods and check one with the other?
- Can you read an 8-bit code that starts with 1 without doubting the sign?
- Do you know the two’s complement range for any number of bits and why it is asymmetric?
- Do you keep the end carry separate from overflow in every addition?
- Have you done the calculations of the four problems and checked the results with the script?
- Have you tried the “Try it yourself” tasks without looking at the theory?
If you answered yes to almost everything, you are ready for the two’s complement exercises, where I no longer give you the calculations done. See you there!
Next step: exercisesTwo’s complement — solved exercises with an 8-bit odometer, additions that overflow, a 6-bit panel and a practice quiz (and the 9C I read as 156)Did this article help you? Here is how you can support SergioLearns →



