Binary arithmetic — theory with a lift: additions with carries, subtractions with borrows, sign-magnitude and ones’ complement (and basement 8 in a building with three)
While preparing for the first IC1 midterm I made up an exercise: a lift is on basement level −3 and goes up 5 floors; where does it end up? I encoded −3 in 8-bit sign-magnitude, 10000011, the +5 as 00000101, added them bit by bit as I had always done with unsigned numbers and got 10001000. I read it calmly: sign bit 1, magnitude 0001000. Basement 8. In a building with three basement levels. The answer was floor +2, and anyone who has ever been in a lift knew it. The mistake was not in the addition; it was in believing that a signed addition works like an unsigned one.
This is the third paper topic of Computer Engineering I (IC1). In the previous one, number systems, we learnt to write numbers in binary, octal and hexadecimal. Today we learn to operate with them: adding with carries, subtracting with borrows, adding several numbers in a chain, multiplying and dividing in plain (unsigned) binary and then representing negative numbers with two conventions, sign-magnitude and ones’ complement, and adding with them without the lift ending up on a basement level that does not exist. Two’s complement, the format computers actually use, has its own topic next. Everything revolves around the lift of a shopping center. Afterwards, in the binary arithmetic practice we solve the typical exam problems step by step, and in the exercises it is your turn.
Table of Contents
Before you start. From number systems you need just one thing, but you need it well: that each position of a binary number has a weight (1, 2, 4, 8, 16, 32, 64 and 128 with 8 bits) and that to convert to decimal you only have to add the weights of the positions holding a 1. That is how you check every calculation in this topic. It will also help to remember that after the binary point the weights keep halving (0.5 and 0.25) and that 8 unsigned bits go up to 255. Columns are counted from the right starting at 0: column 0 is the one with weight 1 and column 7, the one with weight 128. The topic has two halves. In the first, unsigned numbers (the scale and the trip counter): adding with carries, adding with a binary point, subtracting with borrows, adding several numbers in a chain, overflow, multiplying and dividing. In the second, signed numbers (the floor): sign-magnitude, ones’ complement and what overflow looks like when there is a sign. At the end, a table comparing the three formats and two Python simulators.
The shopping center lift
The building has three car park levels (−1, −2 and −3), floor 0 at street level and twelve floors above. The lift stores several numbers, each one in an 8-bit register, and they are not all of the same kind:
- The scale measures the kilos inside. There are never −20 kg: it is an unsigned number (plain binary, the one from the previous topic), from 0 to 255 with 8 bits.
- The trip counter counts how many times it has moved. It cannot be negative either.
- The floor does need a sign: −3 and +3 are very different places.
Look at the drawing: the first two only say «how much»; the third says «how much» and «which way». The first half of the topic is arithmetic with unsigned numbers (the scale and the counter) and the second, how signed numbers are stored and added (the floor).
In a computer, all the numbers in a register have the same number of bits. That idea, which looks like a detail, is what explains overflow, ranges and why the sign bit is always in the same position. In this topic almost everything uses 8 bits, as in the exam.
Adding in binary
Binary addition is primary school addition with only two digits. There are four basic rules, and the last one changes everything:
| Operation | Sum | Carry | Written |
|---|---|---|---|
| 0 + 0 | 0 | 0 | 00 |
| 0 + 1 | 1 | 0 | 01 |
| 1 + 0 | 1 | 0 | 01 |
| 1 + 1 | 0 | 1 | 10 |
Why does 1 + 1 give 10? Because there is no 2 in binary: as soon as you reach two, the column goes back to 0 and you carry 1 to the next one, just as in decimal 9 + 1 leaves a 0 and you carry 1. That «carry one» is the carry.
As soon as a carry appears, in the next column you no longer add two bits but three:
| Operation | Sum | Carry | Written |
|---|---|---|---|
| 1 + 0 + 0 | 1 | 0 | 01 |
| 1 + 0 + 1 | 0 | 1 | 10 |
| 1 + 1 + 0 | 0 | 1 | 10 |
| 1 + 1 + 1 | 1 | 1 | 11 |
The 1 + 1 + 1 case is the one people forget most: that is three, and three in binary is 11, so you write 1 and carry 1.
Adding with carries
Two people get into the lift: one of 75 kg and one of 68 kg. The scale adds and :
You add from right to left, column by column. The first six columns are easy (1 + 0, 1 + 0, 0 + 1, 1 + 0, 0 + 0, 0 + 0). In column 6 there is 1 + 1: you write 0 and the carry goes up to column 7, where 0 + 0 + 1 gives 1. Result: 10001111. Look at the drawing: the carry is written above the column it goes to, just like at school.
Check with the sum of weights: 10001111 = 128 + 8 + 4 + 2 + 1 = 143, and 75 + 68 = 143.
The same addition, column by column
An 8-bit addition is eight small additions, one per column, and in each one you only have to answer two questions: which bit you write and whether you carry anything to the next one. The scale’s addition, slowly:
| Column (weight) | 75 | 68 | Carry in | Total | You write | You carry | Why |
|---|---|---|---|---|---|---|---|
| 0 (1) | 1 | 0 | 0 | 1 | 1 | 0 | 1 + 0 is one, it fits in a bit |
| 1 (2) | 1 | 0 | 0 | 1 | 1 | 0 | Same as the previous one |
| 2 (4) | 0 | 1 | 0 | 1 | 1 | 0 | Order does not matter: 0 + 1 = 1 + 0 |
| 3 (8) | 1 | 0 | 0 | 1 | 1 | 0 | No carry from column 2, just two bits |
| 4 (16) | 0 | 0 | 0 | 0 | 0 | 0 | Nothing to add |
| 5 (32) | 0 | 0 | 0 | 0 | 0 | 0 | Nothing to add |
| 6 (64) | 1 | 1 | 0 | 2 | 0 | 1 | Two in binary is 10: you write the 0 and the 1 goes up |
| 7 (128) | 0 | 0 | 1 | 1 | 1 | 0 | Here three bits are added: the two in the column and the carry |
Read from bottom to top, the «You write» column gives 10001111. Look at column 6: twice 64 is 128. That is exactly what a carry is: nothing is lost, the value moves to the column next door, which is worth twice as much.
Check you’ve got it: a third person of 82 kg (01010010) gets in. Add 10001111 + 01010010 column by column. What does the scale show?
Column 0: 1 + 0 = 1, no carry. Column 1: 1 + 1 = 10, you write 0 and carry 1. Column 2: 1 + 0 + 1 = 10, 0 again and carry 1. Column 3: 1 + 0 + 1 = 10, 0 and carry 1. Column 4: 0 + 1 + 1 = 10, 0 and carry 1. Column 5: 0 + 0 + 1 = 1, nothing to carry any more. Column 6: 0 + 1 = 1. Column 7: 1 + 0 = 1. Result: 11100001. Check with the weights: 128 + 64 + 32 + 1 = 225, and 143 + 82 = 225. There are four carries in a row (from column 1 to column 5): this is the case where the row of carries written on top saves you.
Always write the row of carries on top, even if you think you can keep it in your head. In an 8-bit addition with five carries in a row, your head fails; the paper does not.
Adding with a binary point
The lift takes 12.75 seconds to go from the street to floor 12 and 5.5 to open and close the doors. In binary, and . To add them, line up the points and fill in the missing places with zeros:
carries 1 1 1 1
1 1 0 0 . 1 1 12.75
+ 1 0 1 . 1 0 + 5.50
-----------------
1 0 0 1 0 . 0 1 18.25
The point does not move: it comes down into the result as it is. That is why 101.1 is filled with a 0 on the right (101.10): that way the two quarter digits end up in the same column. Check: .
Subtracting in binary
Subtraction also has four rules, and the strange one here is 0 − 1:
| Operation | Difference | Borrow | Written |
|---|---|---|---|
| 0 − 0 | 0 | 0 | 00 |
| 0 − 1 | 1 | 1 | 11 |
| 1 − 0 | 1 | 0 | 01 |
| 1 − 1 | 0 | 0 | 00 |
How can 0 − 1 give 1? Because you are not working out 0 − 1 but 10 − 1 (two minus one): you borrow one unit from the column on the left, which is worth 2 in this column. Two minus one is one, and you are left with a debt. That debt is the borrow or, as the notes call it, the negative carry.
The borrow, first in decimal
Before subtracting in binary, look at the same lift subtraction in decimal, 12 − 5, the way you would do it at school. In the units, 2 − 5 does not reach. You borrow a ten from the column next door: you are no longer working out 2 − 5, but 12 − 5 = 7. That ten has to be paid back, and there are two ways to note it down in the tens column:
- Take 1 off the minuend: the 1 on top becomes 0, and 0 − 0 = 0.
- Add 1 to the subtrahend: you leave the 1 on top as it is and work out 1 − (0 + 1) = 0.
Both give the same, because taking one off the top or putting one more at the bottom changes the difference by the same amount. The IC1 notes use the second one, and it is the one you are going to see in the next example.
In binary, the only thing that changes is how much you borrow. In decimal, the column on the left lends you 10 units of yours; in binary, it lends you 2. That is why 0 − 1 turns into «10 − 1» (two minus one, which gives 1) and 1 − 10 (when you owe one and you are also subtracting a 1) turns into «11 − 10» (three minus two, which gives 1).
Subtracting with borrows
The lift is on floor 12 and is going to floor 5. How many floors does it go down? :
Column 0: 0 − 1, I borrow: 10 − 1 = 1, and I owe 1. Column 1: 0 − (0 + the 1 I owe) = 0 − 1, I borrow again: 1, and I still owe. Column 2: 1 − (1 + 1) = 1 − 10: it does not reach either, I borrow: 11 − 10 = 1, and I owe 1. Column 3: 1 − (0 + 1) = 0. Result: 0111, 7 floors.
In the drawing, the borrows go on top, just like the carries of addition. The notes’ way is to add the borrow to the subtrahend of the next column: in column 2 you do not subtract 1, you subtract 1 + 1 = 10. It is the safest way not to lose any.
The same subtraction, column by column
In each column you subtract the bottom bit plus whatever is owed; if it does not reach, you borrow 2 and the debt moves on to the next column:
| Column (weight) | 12 | 5 | I owe | Subtract | Does it reach? | Calculation | You write | Why |
|---|---|---|---|---|---|---|---|---|
| 0 (1) | 0 | 1 | 0 | 1 | No | 10 − 1 = 1 | 1 | A 0 cannot take away 1: I borrow 2 and I owe 1 |
| 1 (2) | 0 | 0 | 1 | 0 + 1 = 1 | No | 10 − 1 = 1 | 1 | Even though there is a 0 at the bottom, the debt means 1 has to be taken away |
| 2 (4) | 1 | 1 | 1 | 1 + 1 = 10 | No | 11 − 10 = 1 | 1 | Two has to be taken away from a one: I borrow again |
| 3 (8) | 1 | 0 | 1 | 0 + 1 = 1 | Yes | 1 − 1 = 0 | 0 | At last it reaches, and the debt ends here |
Result: 0111, 7 floors. The «I owe» column is the row of borrows in the drawing: three borrows in a row, and each one appears because the previous one could not be paid.
Check you’ve got it: the 68 kg person gets out of the lift. Subtract 10001111 − 01000100 (143 − 68). How many borrows are there and what does the scale show?
Columns 0 to 5: 1 − 0, 1 − 0, 1 − 1, 1 − 0, 0 − 0 and 0 − 0; it always reaches, no borrows: 1, 1, 0, 1, 0, 0. Column 6: 0 − 1 does not reach, you borrow: 10 − 1 = 1, and you owe 1. Column 7: 1 − (0 + 1) = 0, the debt is paid. A single borrow. Result: 01001011 = 64 + 8 + 2 + 1 = 75 kg, the person who stays. And it is the first addition backwards: 75 + 68 = 143, so 143 − 68 had to give 75.
Forgetting that a borrow that lands on a 1 in the subtrahend creates another borrow. In column 2 above, the subtrahend had a 1 and 1 was also owed: you had to subtract 2 from a 1, and that forces another borrow. If you leave it as «1 − 1 = 0», you get 0011 instead of 0111.
Check: 7 + 5 = 12. Subtracting and checking by adding is the cheapest trick in the topic.
Chained additions and subtractions
When you have to add or subtract several numbers, you do it two at a time, using the associative property: . The result of each operation is the first number of the next one.
In the lift, the scale keeps adding each person who comes in: 75, then 68, then 82, then 59. First 75 + 68 = 143 (the earlier sum), then 143 + 82 = 225 and then 225 + 59. Here there is a surprise, and it deserves its own section, because it is the difference between carry and overflow.
Carry and overflow (unsigned)
Before the calculation, the idea in words. The scale’s display has 8 boxes and each box holds one bit, not one more. As long as the weight fits in those 8 boxes, everything is fine. If the sum needs a ninth digit, the circuit works it out, but it has nowhere to store it: that digit falls off and the display only shows the 8 on the right. In decimal the same would happen with a three-digit counter: 999 + 1 would show 000, not because the calculation is wrong, but because 1000 does not fit.
That is all overflow is: the real result exists, but it does not fit in the number of bits the register has. A carry, on the other hand, is a normal part of any addition (you have already seen quite a few in this topic). Only the last one, the one coming out of column 7, is special, because it is the only one with no column to go to.
With 8 bits, the scale only goes up to . The fourth person makes the real weight 284 kg:
In the last addition, 11100001 + 00111011, column 7 produces a carry that has nowhere to go: there is no column 8. That bit is lost and the display is left with 00011100, 28 kg. This is called overflow: the result goes outside the range of representation and what is left in the register is wrong.
With unsigned numbers the warning is easy: if there is an end carry, there is overflow. What you see is exactly the real result minus : 284 − 256 = 28. Look at the drawing: the bits that stay are the usual ones; what has been lost is the ninth.
The addition that overflows, column by column
Follow the scale’s last addition, 225 + 59, like the previous ones:
| Column (weight) | 225 | 59 | Carry in | Total | You write | You carry | Why |
|---|---|---|---|---|---|---|---|
| 0 (1) | 1 | 1 | 0 | 2 | 0 | 1 | 1 + 1 = 10 |
| 1 (2) | 0 | 1 | 1 | 2 | 0 | 1 | 0 + 1 + the carry = 10 |
| 2 (4) | 0 | 0 | 1 | 1 | 1 | 0 | Just the carry |
| 3 (8) | 0 | 1 | 0 | 1 | 1 | 0 | No carry, 0 + 1 |
| 4 (16) | 0 | 1 | 0 | 1 | 1 | 0 | The same |
| 5 (32) | 1 | 1 | 0 | 2 | 0 | 1 | 1 + 1 = 10 |
| 6 (64) | 1 | 0 | 1 | 2 | 0 | 1 | 1 + 0 + the carry = 10 |
| 7 (128) | 1 | 0 | 1 | 2 | 0 | 1 | 1 + 0 + the carry = 10, and that 1 has no column left |
What you write is 00011100 (28) and there is a carry left over from column 7. That carry is worth 256, the weight a column 8 would have if it existed. That is why what you see is the real weight minus 256: 284 − 256 = 28. Up to column 6, the addition is just like all the others; the problem only shows up in the last one.
Check you’ve got it: with 225 kg inside, how much can the fourth person weigh at most so that the scale does not overflow? And what would it show with one more kilo?
The scale goes up to 255, so there is room for 255 − 225 = 30 kg more. 225 + 30 = 255 = 11111111: every box at 1 and no end carry. With one more kilo, 225 + 31 = 256, which in binary is 100000000, nine digits. The leading 1 is the end carry and it is lost, so the display would show 00000000: a lift with four people inside that claims to weigh 0 kg. There is an end carry, so, unsigned, there is overflow.
This is only true for unsigned numbers. With a sign, as we are about to see, the end carry does not tell you whether there is overflow. It is the confusion that costs the most marks in this topic.
Multiplying in binary
The multiplication rules are the easiest of the whole topic: , , and . It is the «and» table: it only gives 1 if both are 1.
The lift takes 5 seconds per floor. How long does it take to go from the street to floor 12? :
The method is the usual one: each bit of the multiplier, starting from the right, gives a partial product, and each partial is written shifted one place to the left. Since you only multiply by 0 or by 1, each partial is either a copy of the multiplicand or a row of zeros. Then all the partials are added. Here: 1100 (bit 0), 0000 shifted (bit 1) and 1100 shifted two places (bit 2). The sum is 111100, 32 + 16 + 8 + 4 = 60 seconds, and 12 × 5 = 60.
The partial products, one by one
The multiplicand is 1100 (12) and the multiplier, 101 (5). You go through the multiplier from right to left, one bit at a time:
| Multiplier bit | Value | Partial product | Shifted | Why |
|---|---|---|---|---|
| Bit 0 | 1 | 1100 | 1100 | Multiplying by 1 is copying; it is the first one, it is not shifted |
| Bit 1 | 0 | 0000 | 00000 | Multiplying by 0 gives zeros; it is shifted one place because that bit is worth 2 |
| Bit 2 | 1 | 1100 | 110000 | A copy again, shifted two places because that bit is worth 4 |
Then the three partials are added like any other addition, with the row of carries on top: 1100 + 00000 + 110000. Here no column has two ones, so there are no carries and you get 111100 straight away. In decimal it is the same as thinking 12 × 5 = 12 × 1 + 12 × 4 = 12 + 48 = 60: each 1 in the multiplier contributes a copy of the multiplicand with its weight, and each 0 contributes nothing. You can skip the rows of zeros when adding, but it is worth writing them at first so you do not get a shift wrong.
Multiplying by 10 in binary (by 2) is adding a 0 on the right, just as multiplying by ten in decimal. That is why the partials are shifted: bit 2 of the multiplier is worth 4, and multiplying by 4 is adding two zeros.
Dividing in binary
Division is school long division, with one big advantage: at each step there are only two possible answers. Either the divisor fits in what you have (quotient digit 1, and you subtract) or it does not fit (digit 0, and you bring down the next bit).
There are 27 people queuing for the lift and 6 fit per trip. :
You bring down bits from the left until you have something equal to or greater than 110: 1 does not reach, 11 does not either, 110 does. Quotient 1, you subtract 110 − 110 = 0. You bring down the next bit, 1: it does not reach (quotient 0). You bring down the last one, 11: it still does not reach 110 (quotient 0). No bits left: the quotient is 100 (4) and the remainder 11 (3).
The check for any division: quotient × divisor + remainder = dividend. Here, 4 · 6 + 3 = 27. In the lift: 4 full trips and 3 people waiting for a fifth one.
Division, step by step
The same sharing out of the queue, in a table. «What you have» is the piece of the dividend, 11011, in front of you at each step:
| Step | What you have | Does 110 fit? | Quotient digit | What you do | Why |
|---|---|---|---|---|---|
| 1 | 1, 11, 110 | With 110, yes | 1 | 110 − 110 = 0 | You bring down bits until the divisor fits; before that the quotient has not started yet |
| 2 | 0 and you bring down a 1: 1 | No | 0 | Nothing to subtract | 1 is less than 110; the 0 is written all the same |
| 3 | 1 and you bring down the last 1: 11 | No | 0 | Nothing to subtract | 11 (3) is less than 110 (6) |
| End | 11 | No bits left | What you have is the remainder |
Quotient 100 (4), remainder 11 (3). To know whether it fits you only have to compare: you do not need any times table, because the divisor fits once or not at all.
Check you’ve got it: the lift took 111100 seconds (60) to go up 12 floors at 101 (5) seconds per floor. Work out 111100 ÷ 101. Do you get 12?
You bring down bits: 1 does not reach 101, 11 does not either, 111 does. First digit 1, and 111 − 101 = 10. You bring down the next bit, 1: you have 101, which fits exactly; digit 1 and 101 − 101 = 0. You bring down a 0: 0 does not reach, digit 0. You bring down the last 0: it does not either, digit 0. Quotient 1100 = 12 and remainder 0. Check: 12 · 5 + 0 = 60. It is the earlier multiplication undone, and the remainder 0 tells you the division is exact.
Writing only the 1s in the quotient (when the divisor fits) and skipping the 0s (when it does not). Every bit you bring down is a quotient digit, whether it is 0 or 1. If you skip them, the quotient has fewer digits and is worth much less.
Integers: magnitude and sign
Everything so far was plain binary: unsigned numbers. But the lift also needs to know it is on −3. An integer has two features: its magnitude (how much it is worth, the 3) and its sign (whether it is positive or negative).
We are going to see two formats to store integers in binary (in the next topic, a third one, two’s complement). In all of them:
- The most significant bit, the leftmost one, is the sign: 0 for positive and 1 for negative.
- All numbers have the same number of bits, so the sign bit has a fixed position. With 8 bits, it is always bit 7.
This has a consequence worth remembering: you cannot drop the zeros on the left. In plain binary 00000101 and 101 are the same; in a signed format, the first bit is information. Always write all 8 bits.
Sign-magnitude
It is the most natural way to write a signed number, the one you would come up with knowing nothing: one bit for the sign and the rest for the magnitude, in plain binary.
How it is written
A negative number has the same magnitude bits as its positive version, and only the sign bit changes. With 8 bits, 1 for the sign and 7 for the magnitude:
| Floor | Sign | Magnitude (7 bits) | Sign-magnitude |
|---|---|---|---|
| +12 | 0 | 0001100 | 00001100 |
| +5 | 0 | 0000101 | 00000101 |
| 0 | 0 | 0000000 | 00000000 |
| −3 | 1 | 0000011 | 10000011 |
To read it, the other way round: the first bit gives the sign and the other seven are read as plain binary. 10000011 → sign − and magnitude 3 → −3.
The range and the two zeros
With n bits, the magnitude has bits, so it goes up to . The range is symmetric:
With 8 bits, from −127 to +127. With 4 bits, from −7 to +7. Look at it with the 16 four-bit codes read in three ways:
Look at the sign-magnitude column: code 1000 is −0. Negative sign and zero magnitude. In sign-magnitude there are two zeros, 0000 and 1000, which mean the same thing. That is why 16 codes only represent 15 different values: from −7 to +7. With 8 bits, 00000000 and 10000000: the lift’s ground floor would have two buttons.
Adding and subtracting in sign-magnitude
Here is the basement 8 from the beginning. What I did was this:
I added the bits as if they were unsigned numbers and the sign bit was added like any other. In sign-magnitude you do not add blindly: first you look at the signs. The rule in the notes is:
- Same sign: add the magnitudes and keep the sign. (−3) + (−4): 3 + 4 = 7, sign − → 10000111.
- Different signs: subtract the smaller magnitude from the larger one and use the sign of the larger. (−3) + (+5): 5 − 3 = 2, the larger one is the 5, positive → 00000010, floor +2.
Subtracting is adding the opposite: to subtract, flip the sign bit of the subtrahend and apply the same rule. (+5) − (+12) = (+5) + (−12): different signs, 12 − 5 = 7, sign of the 12 (−) → 10000111, −7.
Multiplying and dividing in sign-magnitude
Much calmer: the magnitudes are multiplied or divided (as in plain binary) and the sign comes from the signs: same sign, positive; different signs, negative. It is the usual rule of signs. (−3) × (+4): 3 × 4 = 12 and different signs → 10001100, −12.
Drawbacks of sign-magnitude
It is easy for us to read, but awkward for a circuit:
- It has two representations of zero (00000000 and 10000000), with the same meaning. A circuit that compares with zero has to check both.
- To add, signs and magnitudes have to be compared before deciding whether to add or subtract, and that makes the circuits slow.
- You need circuits that can add and subtract. Multiplication and division can be built from additions and subtractions, but both operations must be there.
Ones’ complement
Ones’ complement (C1) was born to get rid of the comparison: subtracting becomes adding, without asking anything first.
How it is written
The ones’ complement of a binary number is obtained by flipping all its bits: 0s become 1s and 1s become 0s. With that, the format is:
- Positive numbers are written as in sign-magnitude (and as in plain binary): +3 = 00000011.
- Negative numbers are the ones’ complement of the positive: −3 = 11111100.
The sign bit still works: all negatives start with 1 and all positives with 0. But the rest of the bits of a negative number are no longer the magnitude: 11111100 is not «minus 1111100».
| Floor | Positive (8 bits) | C1 |
|---|---|---|
| +12 | 00001100 | 00001100 |
| −3 | 00000011 | 11111100 |
| −12 | 00001100 | 11110011 |
Its decimal value
There are two ways to read a negative C1 number, and it is worth knowing both:
- Flip and add the sign. 11111100 → flip → 00000011 = 3 → it was negative → −3. It is the quickest.
- Weights with a negative sign, then add 1, as in the notes: the sign bit weighs (with 8 bits, −128), the other bits their usual weights, and 1 is added to the total. In the drawing: −128 + 64 + 32 + 16 + 8 + 4 = −4, and −4 + 1 = −3.
For positive numbers there is nothing to do: they are read as plain binary. 00001100 = 12.
The range and the two zeros (again)
The range is the same as in sign-magnitude, : from −127 to +127 with 8 bits and from −7 to +7 with 4. Look at the last column of the 4-bit table again: code 1111 is the C1 of 0000, that is, −0. Ones’ complement also has two zeros, 00000000 and 11111111. And notice the order: in C1 the negatives go from 1000 (−7) to 1111 (−0), the opposite of sign-magnitude.
Subtracting by adding
Here is the beauty of C1: subtraction is unnecessary. To subtract, you add the ones’ complement of the subtrahend to the minuend, without looking at signs or comparing anything.
The lift is on floor 5 and goes down 12 floors: .
carries 1 1 1
0 0 0 0 0 1 0 1 +5
+ 1 1 1 1 0 0 1 1 −12
-----------------
1 1 1 1 1 0 0 0 −7
There are carries inside (columns 1, 2 and 3), but no end carry: there is nothing to give back. The result starts with 1, so it is negative: flip it, 00000111 = 7, so it is −7. The calculation is right: 5 − 12 = −7. That the building only has three basement levels is the problem statement’s fault, not ones’ complement’s.
The end carry that wraps around
Now the case from the beginning, in C1: the lift is on −3 and goes up 5.
11111100 + 00000101 gives 00000001 and an end carry that does not fit in 8 bits. In C1 that carry is not thrown away: it is added back to the result on the right (the end-around carry). 00000001 + 1 = 00000010: floor +2, at last.
Why is it needed? Because the ones’ complement of a number x with 8 bits is , not . When you add a negative number you «overshoot» by one unit and get a carry worth 256; giving that carry back as a 1 on the right corrects exactly that extra unit. It is the same calculation seen in two ways.
The carry that wraps around, column by column
The addition −3 + 5 in C1, slowly, to see where that end carry comes from:
| Column | −3 (11111100) | +5 (00000101) | Carry in | You write | You carry | Why |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 1 | 0 | 0 + 1 |
| 1 | 0 | 0 | 0 | 0 | 0 | Nothing to add |
| 2 | 1 | 1 | 0 | 0 | 1 | 1 + 1 = 10 |
| 3 | 1 | 0 | 1 | 0 | 1 | 1 + the carry = 10 |
| 4 | 1 | 0 | 1 | 0 | 1 | The same: the carry keeps pushing to the left |
| 5 | 1 | 0 | 1 | 0 | 1 | The same |
| 6 | 1 | 0 | 1 | 0 | 1 | The same |
| 7 | 1 | 0 | 1 | 0 | 1 | The same, and that 1 leaves the register |
| Wrap-around | 00000001 + 1 = 00000010 | The end carry is added to column 0 |
The ones of −3 behave like a row of dominoes: as soon as column 2 carries one, the carry runs through all the ones until it comes out on the left. That carry is worth 256 and is left over; the provisional result, 00000001, has come out one unit short. Giving it back on the right fixes both things at once.
Throwing away the end carry in C1. Throwing it away is what you do in two’s complement (the next topic); in C1 you have to add it. If you throw it away, the result is one unit short: in the example, floor +1.
Overflow in ones’ complement
With a sign there can also be overflow: the result goes outside . But here the end carry is no warning, because we have just seen it appear in a perfectly correct addition (−3 + 5). The warning is different: the result does not have the sign it should have.
The building’s old lift had a 4-bit panel, from −7 to +7. From floor 6, it goes up 3:
0110 + 0011 = 1001, which starts with 1: the panel says you are in a basement (1001 → flip → 0110 → −6). Two positive numbers cannot add up to a negative one. That is an overflow: 9 does not fit in −7 … +7. And notice that there was no end carry.
The rule, which applies to sign-magnitude and C1 (and two’s complement):
- Overflow can only happen when adding two numbers with the same sign (or subtracting two with different signs, which is the same thing).
- There is overflow if both addends have the same sign and the result has the opposite one: positive + positive = negative, or negative + negative = positive.
- If the addends have different signs, there is never overflow: the result lies between the two.
Drawbacks of ones’ complement
- It still has two zeros (00000000 and 11111111).
- Subtraction, multiplication and division are done with additions, but the adder gets more complicated because sometimes the end carry has to be added to the result: it is a second addition, and that makes the circuits slow.
Both drawbacks have a fix, and the fix is called two’s complement: a single zero, and the end carry is simply thrown away. That is the next topic.
Unsigned, sign-magnitude and C1, side by side
| Unsigned | Sign-magnitude | Ones’ complement | |
|---|---|---|---|
| Range with n bits | |||
| Range with 8 bits | 0 … 255 | −127 … +127 | −127 … +127 |
| −3 with 8 bits | not possible | 10000011 | 11111100 |
| Zeros | one | two (00000000 and 10000000) | two (00000000 and 11111111) |
| How to add | bit by bit | compare signs and magnitudes | bit by bit + end-around carry |
| How to subtract | with borrows | flipping the subtrahend’s sign | adding the subtrahend’s C1 |
| Overflow warning | end carry | impossible sign | impossible sign |
Common mistakes
Adding 1 + 1 + 1 and writing 0 with carry 1. That is three: you write 1 and carry 1.
Adding numbers with a binary point without lining up the points, or with the point shifted in the result. Fill with zeros on the right until both fractional parts have the same number of digits.
Dropping the zeros on the left of a signed number. In 8 bits, +5 is 00000101, not 101: the first bit is the sign.
Saying «there is a carry, so there is overflow» in sign-magnitude or C1. That only works for unsigned numbers. With a sign, overflow is checked in the sign of the result.
Reading a negative C1 number as if it were sign-magnitude. 11111100 is not −124: it is −3. You flip it first.
Check it with Python
Two short simulators that do the calculations as on paper, column by column, so you can check yours. They were run once and this is their real output.
Plain binary: adding, subtracting, multiplying and dividing
# Unsigned binary arithmetic, as on paper: the shopping center lift
def add(a, b, n):
"""Column-by-column addition: returns the n-bit result, the row of carries and the end carry."""
carry, res, row = 0, '', ''
for i in range(n): # from right to left, as by hand
s = (a >> i & 1) + (b >> i & 1) + carry # up to three bits: the two and the carry
row = ('1' if carry else '.') + row # the carry that COMES INTO this column
res, carry = str(s % 2) + res, s // 2
return res, row, carry
def subtract(a, b, n):
"""Column-by-column subtraction with borrows (the «negative carry» of the notes)."""
borrow, res, row = 0, '', ''
for i in range(n):
d = (a >> i & 1) - (b >> i & 1) - borrow
row = ('1' if borrow else '.') + row
borrow = 1 if d < 0 else 0 # 0 - 1: borrow 1 from the next column
res = str(d + 2 * borrow) + res
return res, row
r, f, c = add(75, 68, 8) # two people in the lift: 75 kg and 68 kg
print(f'carries {f}\n 75 + 68 = {r} = {int(r, 2)} (end carry {c})')
r, f, c = add(51, 22, 7) # 1100.11 + 101.1 with two digits after the point (x4)
print(f'carries {f}\n 1100.11 + 101.1 = {r[:5]}.{r[5:]} = {int(r, 2) / 4}')
r, f = subtract(12, 5, 4) # from floor 12 to floor 5: how many floors down?
print(f'borrows {f}\n 1100 - 0101 = {r} = {int(r, 2)}')
weight = 0 # the lift scale stores 8 bits
for p in (75, 68, 82, 59):
r, f, c = add(weight, p, 8)
print(f' {weight:3} + {p:2} -> {r} = {int(r, 2):3} end carry {c}')
weight = int(r, 2)
print(f' the scale shows {weight} kg; the real weight is {75 + 68 + 82 + 59} kg')
partials = [12 << i if 5 >> i & 1 else 0 for i in range(3)] # 1100 x 101
print(' partials:', [format(p, 'b') for p in partials], '->', format(sum(partials), 'b'), '=', sum(partials))
rem, quotient = 0, '' # 11011 / 110 bit by bit (long division)
for bit in '11011':
rem = rem * 2 + int(bit) # bring down the next bit
if rem >= 6:
rem, quotient = rem - 6, quotient + '1' # it fits: subtract and the quotient gets a 1
else:
quotient += '0'
print(f' 11011 / 110 -> quotient {quotient} = {int(quotient, 2)}, rem. {rem:b} = {rem}')
carries 1.......
75 + 68 = 10001111 = 143 (end carry 0)
carries 11.11..
1100.11 + 101.1 = 10010.01 = 18.25
borrows 111.
1100 - 0101 = 0111 = 7
0 + 75 -> 01001011 = 75 end carry 0
75 + 68 -> 10001111 = 143 end carry 0
143 + 82 -> 11100001 = 225 end carry 0
225 + 59 -> 00011100 = 28 end carry 1
the scale shows 28 kg; the real weight is 284 kg
partials: ['1100', '0', '110000'] -> 111100 = 60
11011 / 110 -> quotient 00100 = 4, rem. 11 = 3
What to look at:
- In the row of carries, each
1is the carry that comes into that column (the dots are columns with no carry). In 75 + 68 there is only one, in column 7. - The addition with a point is done by multiplying by 4 (two fractional digits) and adding integers: the computer does not know where the point is either; you place it.
- On the scale, the last addition gives
end carry 1and the display stays at 28: that is unsigned overflow, 284 − 256. - In the division, the quotient comes out as
00100: the zeros on the left are the steps where the divisor did not fit. Every bit you bring down is a digit.
Sign-magnitude and ones’ complement
# Sign-magnitude and ones' complement: the lift panel (floors -3 to 12)
def sm(x, n=8): # sign bit + magnitude in n-1 bits
return ('1' if x < 0 else '0') + format(abs(x), f'0{n - 1}b')
def c1(x, n=8): # negatives: flip every bit of the positive
p = format(abs(x), f'0{n}b')
return p if x >= 0 else ''.join('1' if b == '0' else '0' for b in p)
def value_c1(s): # the sign weighs -2^(n-1) and negatives get 1 added
v = -int(s[0]) * 2 ** (len(s) - 1) + int(s[1:], 2)
return v + 1 if s[0] == '1' else v
def add_c1(a, b):
"""Ones' complement addition: an end carry is added back to the result (end-around)."""
n = len(a); t = int(a, 2) + int(b, 2)
wrap = t >> n # the bit that does not fit
r = format((t & (2 ** n - 1)) + wrap, f'0{n}b')
ovf = a[0] == b[0] and r[0] != a[0] # same sign in, a different sign out
return r, wrap, ovf
for floor in (12, 5, 0, -3):
print(f'floor {floor:3}: SM {sm(floor)} C1 {c1(floor)}')
print('negative zero: SM 10000000 C1', c1(0).replace('0', '1'))
wrong = format(int(sm(-3), 2) + int(sm(5), 2), '08b') # adding SM bit by bit (WRONG)
print('SM bit by bit: -3 + 5 ->', wrong, '= floor', -int(wrong[1:], 2) if wrong[0] == '1' else int(wrong, 2))
print('SM with its rule: different signs -> 5 - 3 = 2 with the sign of 5 ->', sm(2))
for a, b in ((-3, 5), (5, -12)): # up 5 from -3; down 12 from floor 5
r, v, d = add_c1(c1(a), c1(b))
print(f'C1: {c1(a)} + {c1(b)} -> {r} (wrap {v}) = {value_c1(r)} overflow: {d}')
print('old 4-bit panel: range from', -(2 ** 3 - 1), 'to', 2 ** 3 - 1)
r, v, d = add_c1(c1(6, 4), c1(3, 4)) # floor 6, up 3: floor 9?
print(f'C1 4 bits: 0110 + 0011 -> {r} = {value_c1(r)} overflow: {d}')
floor 12: SM 00001100 C1 00001100 floor 5: SM 00000101 C1 00000101 floor 0: SM 00000000 C1 00000000 floor -3: SM 10000011 C1 11111100 negative zero: SM 10000000 C1 11111111 SM bit by bit: -3 + 5 -> 10001000 = floor -8 SM with its rule: different signs -> 5 - 3 = 2 with the sign of 5 -> 00000010 C1: 11111100 + 00000101 -> 00000010 (wrap 1) = 2 overflow: False C1: 00000101 + 11110011 -> 11111000 (wrap 0) = -7 overflow: False old 4-bit panel: range from -7 to 7 C1 4 bits: 0110 + 0011 -> 1001 = -6 overflow: True
What to look at:
- The positives (+12, +5, 0) are written the same way in both formats; −3 changes completely.
- Negative zero exists in both:
10000000in sign-magnitude and11111111in C1. - The bit-by-bit addition in sign-magnitude gives floor −8 (my story); with its rule, +2.
- In C1, −3 + 5 gives
wrap 1and result 2, without overflow; on the 4-bit panel, 6 + 3 gives −6 with overflow and without a carry. Carry and overflow are different things.
Try it in the One number, every form simulator: you do sums and subtractions column by column in unsigned binary, sign-magnitude and ones’ complement, with their carries or borrows, the C1 end-around carry and overflow marked.
Summary
| Concept | The essentials |
|---|---|
| Binary addition | 1 + 1 = 10 and 1 + 1 + 1 = 11: one bit is written and the other is the carry |
| Binary subtraction | 0 − 1 = 1 by borrowing; the borrow is added to the subtrahend of the next column |
| In a chain | Two at a time; each result is the first number of the next one |
| Multiplication | Partials = copy or zeros, shifted; then added |
| Division | At each step, it fits (1 and subtract) or it does not (0 and bring down); quotient × divisor + remainder = dividend |
| Unsigned overflow | There is an end carry: the result does not fit in 0 … |
| Sign-magnitude | Sign bit + magnitude; two zeros; added by comparing signs |
| Ones’ complement | Negatives = flip every bit; two zeros; subtract by adding; the end carry is added back |
| SM and C1 range | : −127 … +127 with 8 bits |
| Signed overflow | Same sign in the addends and the opposite in the result |
Three ideas to take away
- In binary you add, subtract, multiply and divide as in decimal, but with two digits: 1 + 1 carries one, 0 − 1 borrows one. Always check the other way round.
- A signed number has a fixed number of bits and the first one is the sign. Sign-magnitude stores the magnitude as it is; ones’ complement flips the bits of the negatives so that subtracting becomes adding.
- Carry and overflow are not the same: unsigned, the end carry is the warning; with a sign, the warning is a result with an impossible sign.
Now it is time to practice. In the binary arithmetic practice we solve four problems step by step: additions and subtractions with carries, borrows and a binary point, a multiplication and a division, integers in sign-magnitude and C1, and a question in the format of the written exam: a chained addition in ones’ complement.
Next step: practiceBinary arithmetic — practice with guided problems step by step: additions and subtractions, multiplying and dividing, sign-magnitude, ones’ complement and a question in the exam format (and the 3 trips for 109 people)Did this article help you? Here is how you can support SergioLearns →



