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

In a mock exam I did at home there was one of the easy binary divisions: 1101101 divided by 1011. I kept bringing down bits and, every time the divisor fitted, I wrote a 1 in the quotient. It fitted twice, so I wrote 11. Three. It looked odd, so I checked it with the usual trick, quotient times divisor plus remainder: 3 · 11 + 10 = 43. And the dividend was 109. I had done every subtraction right and the quotient wrong, because I had skipped the steps where the divisor did not fit, and each of those steps is a digit too: a 0. The quotient was 1001, nine. If 109 people are waiting for an 11-seat lift, 3 trips are not enough.

This is the practice of the third paper topic of IC1. In the binary arithmetic theory we saw how to add, subtract, multiply and divide in plain binary, what overflow is and how negative numbers are represented in sign-magnitude and ones’ complement, always with the shopping center lift. Here we solve four problems step by step, the way they are done in the written exam: the result is not enough, you have to show the working. For each one you will see the method, the reason for each step, how to check it and the mistakes that come up most. The last one has the exact format of an exam question.

Before you start

Make sure you are clear on:

  • The rules of addition (1 + 1 = 10 and 1 + 1 + 1 = 11) and subtraction (0 − 1 = 1 with a borrow).
  • How to go from decimal to binary and from hexadecimal or octal to binary by grouping bits (you saw it in number systems).
  • How to write a negative number in sign-magnitude (flip the sign bit) and in ones’ complement (flip every bit).
  • That unsigned overflow shows in the end carry and signed overflow in the sign of the result.

If any of them sounds shaky, go over the theory before carrying on.

There is no calculator in the exam. Write the numbers in columns on an imaginary grid (one bit per square) and leave a free row on top for the carries or the borrows. Half the mistakes in this topic are crooked columns.

How to solve a paper problem

The four problems follow the same four-step method. I give it to you once here so I do not repeat it:

  1. Write down the data with its base and its format (unsigned, sign-magnitude, C1) and how many bits are asked for.
  2. Choose the method before calculating: addition with carries, subtraction with borrows, adding the complement, the rule of signs?
  3. Solve in order, showing every step, with the row of carries or borrows written down. In the written exam, a result without working is worth little.
  4. Check in decimal: convert the operands and the result to decimal and do the sum. If it matches, it is right; if not, you know you have to go back.

Problem 1: adding and subtracting in plain binary

Statement. The lift’s log stores several 8-bit unsigned values (with three fractional digits in the times). Calculate in binary and check in decimal:

a) The total time of a journey: 1011.101(2)+110.11(2)1011.101_{(2)} + 110.11_{(2)} seconds.

b) What is left before a platform’s maximum load: 11000101(2)−01101110(2)11000101_{(2)} – 01101110_{(2)} kg.

c) Two people get in, of 85 kg and 63 kg, and on the next floor one of 54 kg gets out: (01010101(2)+00111111(2))−00110110(2)(01010101_{(2)} + 00111111_{(2)}) – 00110110_{(2)}.

The idea

The three parts are column-by-column additions and subtractions; what changes is the detail that complicates them. In a), the points: you have to line them up and fill with zeros so that the fractional parts have the same number of digits. In b), the chained borrows: there are columns where the borrow lands on a 1 in the subtrahend and creates another one. In c), the chain: it is done two at a time, and the result of the addition is the minuend of the subtraction.

Before calculating, estimate. In a) it is about 11 + 7, so it must come out near 18. In b), about 200 − 110, near 90. If you get 300, you have lost a borrow.

Step by step

a) The points are lined up and 110.11 is written as 0110.110 (a zero in front to have the same integer digits and another one at the end for the fractional ones). Then a normal addition from right to left:

carries   1 1 1 1 1
            1 0 1 1 . 1 0 1      11.625
          + 0 1 1 0 . 1 1 0    +  6.750
          -------------------
          1 0 0 1 0 . 0 1 1      18.375

The last two fractional digits have no carry (1 + 0 and 0 + 1). In the first fractional digit, 1 + 1 = 10: you write 0 and the carry crosses the point into the units. From there on there is a run of carries: 1 + 0 + 1, 1 + 1 + 1 (three: write 1 and carry 1), 0 + 1 + 1 and 1 + 0 + 1. The last carry opens a new digit on the left: 10010.011.

b) Subtraction with borrows, from right to left. This is the one to do most calmly:

Problem 1b: a subtraction with chained borrows11000101 minus 01101110: there are borrows in columns 1 to 6, and some fall on a 1 in the subtrahend and create another one; the result is 01010111, 87.borrows111111minuend11000101197subtrahend01101110110−difference0101011187
A borrow that lands on a 1 in the subtrahend turns it into a «2» and forces another borrow.

Column by column (the incoming borrow is added to the subtrahend of that column):

ColumnMinuendSubtrahend + borrowResultBorrows?
0101No
1011Yes
211 + 1 = 21Yes
301 + 1 = 20Yes
400 + 1 = 11Yes
501 + 1 = 20Yes
611 + 1 = 21Yes
710 + 1 = 10No

When you have to subtract 2 from a 0 or a 1, you borrow (the minuend becomes 2 or 3) and you are left with 0 or 1. Result: 01010111.

c) First the addition, 85 + 63:

carries     1 1 1 1 1 1 1
            0 1 0 1 0 1 0 1      85
          + 0 0 1 1 1 1 1 1    + 63
          -----------------
            1 0 0 1 0 1 0 0     148

Seven carries in a row, because 63 is 00111111, all ones. And now the result, 10010100, is the minuend of the subtraction:

borrows     1 1 1 1 1 1
            1 0 0 1 0 1 0 0     148
          − 0 0 1 1 0 1 1 0    − 54
          -----------------
            0 1 0 1 1 1 1 0      94

This subtraction has six borrows in a row, so it is worth seeing it column by column, with the same table as in b):

ColumnMinuendSubtrahend + borrowResultBorrows?Why
0000No0 − 0 = 0
1011YesYou cannot take 1 from a 0: it borrows, becomes 2, and 2 − 1 = 1
211 + 1 = 21YesThe borrow lands on a 1 in the subtrahend: you have to take 2 from a 1, so it borrows; 3 − 2 = 1
300 + 1 = 11YesThe subtrahend is 0, but the pending borrow turns it into 1; 2 − 1 = 1
411 + 1 = 21YesLike column 2: 3 − 2 = 1
501 + 1 = 20Yes0 − 2 cannot be done: it borrows and 2 − 2 = 0
600 + 1 = 11YesLike column 3: 2 − 1 = 1
710 + 1 = 10NoAt last a 1 that covers the borrow: 1 − 1 = 0 and the chain stops

Read from column 7 down to 0: 01011110, 94. The six «Yes» are the six ones in the borrow row. Column 7 not borrowing tells you everything went well: if it borrowed, the subtrahend would be bigger than the minuend and the result would not fit in unsigned binary.

Check

In decimal, with the sum of weights:

  • a) 10010.011(2)=16+2+0.25+0.125=18.37510010.011_{(2)} = 16 + 2 + 0.25 + 0.125 = 18.375, and 11.625+6.75=18.37511.625 + 6.75 = 18.375.
  • b) 01010111 = 64 + 16 + 4 + 2 + 1 = 87, and 197 − 110 = 87. Check the other way round: 87 + 110 = 197.
  • c) 01011110 = 64 + 16 + 8 + 4 + 2 = 94, and 85 + 63 − 54 = 94.

No addition has overflowed: 148 fits in 8 bits (it goes up to 255) and there was no end carry.

Questions you might ask

Why do you have to fill with zeros in a)? Because each column has a weight. If you write 110.11 under 1011.101 without filling, the eighths digit of the first one has no partner and it is easy to end up adding quarters to eighths. With 0110.110 every column has exactly two bits of the same weight.

Can a carry cross the point? Yes, no problem. The point only marks where the negative weights start; two halves (0.1 + 0.1) make a unit, and that unit moves to the units column like any other carry.

In b), why not subtract the borrow from the minuend instead of adding it to the subtrahend? You can, it is equivalent: subtracting 1 at the top or adding 1 at the bottom gives the same difference. The notes add it to the subtrahend and that is what I recommend, because the minuend then never changes and you do not have to cross anything out.

What happens if in c) I do the subtraction before the addition? It may come out right or not. 85 − 54 + 63 gives the same in decimal, but in 8-bit plain binary an intermediate subtraction cannot be negative: if the order takes you to a negative number, unsigned binary cannot store it. That is why the order of the statement is kept.

In b), in column 3 (0 − 1 with a pending borrow), writing 1 and forgetting the new borrow. That lost borrow shows at the end: the result comes out 128 too high or leaves column 7 wrong. A table like the one above on scrap paper saves you the slip.

Try it yourself

  • Calculate 1110.01(2)+101.111(2)1110.01_{(2)} + 101.111_{(2)} (14.25 + 5.875). It should give 10100.001(2)10100.001_{(2)}, 20.125.
  • Calculate 10000000(2)−00000001(2)10000000_{(2)} – 00000001_{(2)}. It is the subtraction with the most consecutive borrows in 8 bits: seven. It should give 01111111, 127.

Problem 2: multiplying and dividing

Statement. The building’s emergency staircase has 13 flights of 23 steps, and there are 109 people queuing for an 11-seat lift. In binary:

a) Calculate the number of steps: 10111(2)×1101(2)10111_{(2)} \times 1101_{(2)}.

b) Calculate how many full trips are needed and how many people are left: 1101101(2)÷1011(2)1101101_{(2)} \div 1011_{(2)}, with quotient and remainder.

The idea

Multiplication is a sum of partial products: for each bit of the multiplier, a copy of the multiplicand (if the bit is 1) or a row of zeros (if it is 0), shifted as many places as the bit number. Division is long division: bits are brought down, and at each step the divisor fits (digit 1 and subtract) or does not fit (digit 0 and bring down another one). The quotient has one digit for every bit brought down from the first one that gives something.

Before multiplying, see which number has fewer ones and use it as the multiplier: you will have fewer partials to add. Here 1101 has three; 10111, four.

Step by step

a) The multiplier 1101 has bits 0, 2 and 3 set to 1 and bit 1 set to 0:

                    1 0 1 1 1        23
                  ×   1 1 0 1      × 13
          -------------------
                    1 0 1 1 1      (bit 0 = 1: copy)
                  0 0 0 0 0        (bit 1 = 0: zeros)
                1 0 1 1 1            (bit 2 = 1: copy shifted 2)
            + 1 0 1 1 1              (bit 3 = 1: copy shifted 3)
          -------------------
            1 0 0 1 0 1 0 1 1       299

The tricky part is the final addition of four rows: do it two at a time if you get muddled (10111 + 1011100 = 1110011; then + 10111000 = 100101011). That way each addition has at most one carry per column.

What each row does, in decimal:

Multiplier bitValueRowIn decimalWhy
011011123Copy with no shift: 23 × 1
1000000 (shifted 1)0A 0 in the multiplier gives a row of zeros
21101110092Shifting 2 places to the left is multiplying by 4: 23 × 4
3110111000184Shifting 3 places is multiplying by 8: 23 × 8

Sum: 23 + 92 + 184 = 299, which is 23 × (1 + 4 + 8) = 23 × 13. The ones in 1101 sit at the weights 1, 4 and 8, and that is why each copy is shifted the way it is: shifting one place to the left in binary is multiplying by 2, just as adding a zero in decimal is multiplying by 10. The two partial sums can be checked the same way: 1110011 = 115 = 23 + 92, and 100101011 = 299 = 115 + 184.

b) The division, step by step:

Problem 2b: 1101101 divided by 1011Long division of 1101101 (109) by 1011 (11): the divisor fits twice along the dividend; the quotient is 1001, 9, and the remainder 1010, 10.1101101101111011011−101011011−1001quotient1010remainder109 ÷ 11= 9 remainder 109 · 11 + 10 = 109
When the partial remainder is still smaller than the divisor, write a 0 in the quotient and bring down another bit.
Bits brought downDoes 1011 fit?Quotient digitWhat is left
1No(0)1
11No(0)11
110No(0)110
1101Yes11101 − 1011 = 10
101No0101
1010No01010
10101Yes110101 − 1011 = 1010

The first three digits in brackets are leading zeros, which are not written. The quotient is 1001 and the remainder 1010. Look at the two middle rows: the divisor does not fit, and that is why a 0 is written in the quotient. Those two zeros were exactly what I skipped in the mock exam.

Check

  • a) 100101011(2)=256+32+8+2+1=299100101011_{(2)} = 256 + 32 + 8 + 2 + 1 = 299, and 23 × 13 = 299.
  • b) Quotient 1001(2)=91001_{(2)} = 9, remainder 1010(2)=101010_{(2)} = 10. Check: 9 · 11 + 10 = 99 + 10 = 109. And the remainder is smaller than the divisor (10 < 11), as it must be. In the lift: 9 full trips and 10 people for a tenth trip.

Questions you might ask

Why does the product have 9 bits if the factors have 5 and 4? Because the product of an m-bit number by an n-bit one can have up to m + n bits. Here 5 + 4 = 9. If you had to store it in 8 unsigned bits, it would overflow.

Do I have to write the row of zeros for bit 1? It is not compulsory, but in the exam I recommend writing it: that way it is clear that the next row is shifted two places and not one, which is the most common mistake when you skip it.

How do I know if the divisor «fits»? By comparing them as numbers: what you have must be equal to or greater than the divisor. If they have the same number of digits, look from left to right for the first different bit. 1010 against 1011: the last bit decides, and 1010 is smaller.

And if my remainder is greater than the divisor? Then the divisor fitted once more at some step and you missed it. The remainder is always smaller than the divisor; otherwise the division is wrong.

Writing only the 1s in the quotient and skipping the 0s of the steps where the divisor does not fit. The quotient comes out with fewer digits and much smaller. The check quotient × divisor + remainder always catches it.

Try it yourself

  • Multiply 1101(2)×1011(2)1101_{(2)} \times 1011_{(2)} (13 × 11). It should give 10001111(2)10001111_{(2)}, 143, the same number as the scale in the theory.
  • Divide 10001111(2)÷1101(2)10001111_{(2)} \div 1101_{(2)}. It should give quotient 1011 and remainder 0: the division undoes the multiplication.

Problem 3: integers in sign-magnitude and ones’ complement

Statement. The lift panel of a building with a deep car park stores signed heights in 8 bits.

a) Write +45 and −45 in sign-magnitude and in ones’ complement.

b) The panel shows 10101100. Which number is it if it is in sign-magnitude? And in ones’ complement? And if it were unsigned?

c) What is the range of sign-magnitude and of C1 with 8 bits? And the unsigned one?

d) Calculate in sign-magnitude (−45) + (+20) and (−45) + (−20).

The idea

Everything comes from what the first bit means and what to do with the other seven. In sign-magnitude, the seven are the magnitude as it is. In C1, for negatives you have to flip them to see the magnitude. And unsigned, there is no sign bit: all eight are magnitude. To add in sign-magnitude, the rule: same sign, add the magnitudes; different signs, subtract them and the sign of the larger one wins.

Step by step

a) First the positive: 45 = 32 + 8 + 4 + 1 = 0101101 in 7 bits, and with the sign bit, 00101101. That is +45 in both formats (positives do not change). For −45:

  • Sign-magnitude: only the sign bit changes → 10101101.
  • Ones’ complement: every bit of 00101101 is flipped → 11010010.

b) The same code, three readings:

FormatHow to read itValue
Sign-magnitudesign 1 (−), magnitude 0101100 = 32 + 8 + 4−44
Ones’ complementnegative: flip → 01010011 = 64 + 16 + 2 + 1 = 83−83
Unsigned128 + 32 + 8 + 4172

Check of the C1 with the weights from the notes: −128 + 32 + 8 + 4 = −84, and −84 + 1 = −83.

c) With n = 8 bits: sign-magnitude and C1, from −(27−1)=−127-(2^7 – 1) = -127 to 27−1=1272^7 – 1 = 127; unsigned, from 0 to 28−1=2552^8 – 1 = 255. The two signed formats have 255 different values because zero appears twice.

d) (−45) + (+20): different signs, so the magnitudes are subtracted, 45 − 20 = 25, and the sign of the larger one, the 45 (−), is used:

magnitudes   0 1 0 1 1 0 1      45
           − 0 0 1 0 1 0 0    − 20
           ---------------
             0 0 1 1 0 0 1      25    → with sign −: 1 0 0 1 1 0 0 1

(−45) + (−20): same sign, so the magnitudes are added, 45 + 20 = 65, and the − sign is kept:

magnitudes   0 1 0 1 1 0 1      45
           + 0 0 1 0 1 0 0    + 20
           ---------------
             1 0 0 0 0 0 1      65    → with sign −: 1 1 0 0 0 0 0 1

Check

  • a) 10101101: sign −, 0101101 = 45 → −45. 11010010: flip → 00101101 = 45 → −45.
  • d) 10011001: sign −, 0011001 = 16 + 8 + 1 = 25 → −25, and −45 + 20 = −25. 11000001: sign −, 1000001 = 64 + 1 = 65 → −65, and −45 − 20 = −65. Both magnitudes fit in 7 bits (up to 127): there is no overflow.

Questions you might ask

Why is the same code worth −44, −83 and 172? Because bits do not carry their format written on them. Just as «244» is not a number without its base (we saw it in the previous topic), 10101100 is not a number without its format. In the exam, the statement always says which one it is: underline it.

In the addition of magnitudes in d), what if I get an 8-bit magnitude? Then it does not fit in the 7 magnitude bits: it is an overflow. It would happen, for example, with (−100) + (−40): 140 cannot be written in 7 bits.

Can I do d) in C1 instead of sign-magnitude? Yes, and it is a good exercise: 11010010 + 00010100 = 11100110, which flipped is 00011001, −25. The result is the same number, written in another format. But if the statement asks for sign-magnitude, you answer in sign-magnitude.

In a), writing −45 in C1 by flipping only the 7 magnitude bits and putting a 1 in front, as if it were sign-magnitude. In C1 you flip everything, including the positive’s sign bit (which turns from 0 into 1 on its own).

What would happen if in d) you added the two sign-magnitude codes as they are, as if they were unsigned?

With (−45) + (+20): 10101101 + 00010100 = 11000001. Read in sign-magnitude, sign − and magnitude 1000001 = 64 + 1 = 65: you get −65, and it should have been −25.

With (−45) + (−20): 10101101 + 10010100 = 101000001, which already has nine bits. If you keep the 8 on the right, 01000001 starts with 0: +65, a positive number after adding two negatives.

The mistake is the same in both cases: the sign bit has no weight, it only says whether the number is positive or negative, and adding it as if it were worth 128 makes no sense. On top of that, in (−45) + (+20) the magnitudes are not added, they are subtracted. That is why sign-magnitude keeps the two things apart, as in the step by step: first you look at the signs and then you work only with the seven magnitude bits.

Try it yourself

  • Write −100 in 8-bit sign-magnitude and C1. It should give 11100100 and 10011011.
  • Which number is 11111111 in sign-magnitude? And in C1? (−127 and −0).

Problem 4: a question in the exam format

Statement (as in the written exam, with its answer box):

Given the chained addition ((−37(10)+(−2A(16)))+15(8))((-37_{(10)} + (-\mathrm{2A}_{(16)})) + 15_{(8)}), translate the numbers into 8-bit ones’ complement binary code, carry out the chained addition and state whether overflow occurs. [1 point] Result of the chained addition in 8-bit ones’ complement: ________ · Does overflow occur? Yes / No

The idea

There are three chained tasks and it is worth separating them on the page: translate each number into 8-bit binary (each one comes in a different base), convert the negatives into C1 and add two at a time, checking in each addition the end carry (which in C1 is given back) and the sign (which tells you whether there is overflow). The question is whether there is overflow in the chain: if there is in either of the two additions, the answer is Yes.

2A in hexadecimal and 15 in octal do not need to go through decimal: with groups of 4 bits (hex) and 3 bits (octal) they come out directly in binary. You only convert to decimal to check.

Step by step

1. Translate the numbers into 8-bit binary (magnitudes):

  • 37(10)37_{(10)}: 32 + 4 + 1 → 00100101.
  • 2A(16)\mathrm{2A}_{(16)}: 2 = 0010, A = 1010 → 00101010 (= 42).
  • 15(8)15_{(8)}: 1 = 001, 5 = 101 → 001101 → with 8 bits, 00001101 (= 13).

2. The negatives in ones’ complement (flip every bit):

  • −37 → 11011010
  • −42 → 11010101
  • +13 stays as it is: 00001101.

3. First addition: (−37) + (−42).

Problem 4: the chained addition in ones' complementFirst addition: 11011010 (−37) plus 11010101 (−42) gives 10101111 with an end carry, which is added and leaves 10110000, −79. Second addition: 10110000 plus 00001101 (13) gives 10111101, −66, with no carry.1) (−37) + (−42)carries11−3711011010−37−2A₁₆ = −4211010101−42+1end-around +1 →result10110000−79101011112) (−79) + 13carriesthe previous one10110000−7915₈ = 1300001101+13+result10111101−66no overflow: the signs of the results are the expected ones (−79 and −66 fit in −127 … +127)
Each addition is checked separately: end carry, end-around and sign.
carries     1   1
            1 1 0 1 1 0 1 0     −37
          + 1 1 0 1 0 1 0 1     −42
          -----------------
          1 1 0 1 0 1 1 1 1
                        + 1   (end-around: add the end carry)
          -----------------
            1 0 1 1 0 0 0 0     −79

There is an end carry, so it is added back on the right: 10101111 + 1 = 10110000. Overflow? Both addends are negative and so is the result (it starts with 1): no. Value: flip 10110000 → 01001111 = 79 → −79.

The first addition, column by column (column 0 is the one on the right):

Column−37−42Carry inSumWriteCarry outWhy
0010110Only one digit is 1
1100110Same
2010110Same
3100110Same: the first four columns carry nothing
41102 (10)01First carry
5001110Only the carry: you write it and carry nothing
61102 (10)01Second carry
71113 (11)11Three ones: you write 1 and the 1 you carry has no column left: it is the end carry

The two inner carries are the ones written above columns 5 and 7 in the carry row. The 8 bits of the «Write» column (from 7 down to 0) give 10101111, and the end carry is added back on the right: 10101111 + 1 = 10110000.

Why does the end carry come back in C1? Because a negative −x is stored as 11111111 − x, that is, 255 − x: −37 is 255 − 37 = 218 = 11011010, and −42 is 255 − 42 = 213 = 11010101. Adding them gives 218 + 213 = 431, and dropping the end carry (which is worth 256) leaves 175 = 254 − 79: one less than −79 in C1, which is 255 − 79 = 176. Adding the carry back on the right returns exactly that unit: 176 = 10110000.

4. Second addition: (−79) + 13.

            1 0 1 1 0 0 0 0     −79
          + 0 0 0 0 1 1 0 1     +13
          -----------------
            1 0 1 1 1 1 0 1     −66

There is no carry at all. The addends have different signs, so there cannot be overflow. Value: flip 10111101 → 01000010 = 66 → −66.

Answer: result 10111101; overflow: No.

Check and how it is marked

The check is in decimal: −37 − 42 + 13 = −66, and 10111101 in C1 is −66. Also, each intermediate result has to be within the 8-bit range, −127 … +127: −79 and −66 are, so the «No» answer is consistent.

The exam does not publish how it splits the point, but what is usually valued is this (a guideline split, so you know where you can lose tenths):

PartWhat has to be shownGuideline
TranslationThe three numbers in 8-bit binary, with their working (groups of bits or divisions)0.3
C1The negatives flipped0.1
First additionCarries, end carry and its end-around, result0.3
Second additionResult0.1
OverflowAnswer in the box and why (signs)0.2

The exam instructions say that a question without the answer in its box, or without the working on the sheets, gets no marks. Write both. And write the 8 bits of the result: 10111101, not «−66».

Throwing away the end carry of the first addition (as in two’s complement) and carrying on with 10101111. The second addition comes out −67 instead of −66, and everything after that drags the error along. In C1, the end carry always comes back.

Try it yourself

Do the same question with ((−70(10)+(−3C(16)))+12(8))((-70_{(10)} + (-\mathrm{3C}_{(16)})) + 12_{(8)}). You should find that the first addition, (−70) + (−60) = −130, overflows: two negatives give 01111101, which starts with 0. If you carry on, the final result is 10000111 (−120), which happens to match the decimal calculation, −70 − 60 + 10 = −120. But the question is whether overflow occurs in the chain, and the answer is Yes: it did in the first addition.

Common mistakes in this practice

  • Crooked columns. A bit shifted by one column changes the result completely. Write on a grid and with all 8 bits.
  • The 1 + 1 + 1. That is three: write 1 and carry 1. In additions with lots of ones (like 85 + 63) it comes up several times.
  • The lost borrow. Especially when it falls in a column with a 1 in the subtrahend: there you have to subtract 2 and there is another borrow.
  • The zeros in the quotient. Every bit brought down is a digit: if the divisor does not fit, write 0.
  • Mixing formats. Reading a C1 number as sign-magnitude, or writing the negative in C1 by flipping only the magnitude.
  • The end carry in C1. It is not thrown away: it comes back. And it does not mean overflow: the sign tells you that.

Check it with Python

All the calculations of this practice, done by a script that adds and subtracts column by column, just as you do on paper. It was run once and this is its real output:

# Problems of the binary arithmetic practice: every calculation, once
def add(a, b, n):                              # n-bit result, row of carries and end carry
    c, r, f = 0, '', ''
    for i in range(n):
        s = (a >> i & 1) + (b >> i & 1) + c
        f, r, c = ('1' if c else '.') + f, str(s % 2) + r, s // 2
    return r, f, c

def subtract(a, b, n):                         # n-bit result and row of borrows
    p, r, f = 0, '', ''
    for i in range(n):
        d = (a >> i & 1) - (b >> i & 1) - p
        f = ('1' if p else '.') + f
        p = 1 if d < 0 else 0
        r = str(d + 2 * p) + r
    return r, f

def c1(x, n=8):
    s = format(abs(x), f'0{n}b')
    return s if x >= 0 else ''.join('10'[int(b)] for b in s)

def val_c1(s):
    return int(s, 2) if s[0] == '0' else -int(''.join('10'[int(b)] for b in s), 2)   # negative: flip it

def add_c1(a, b):
    n = len(a); t = int(a, 2) + int(b, 2); v = t >> n
    r = format((t & (2 ** n - 1)) + v, f'0{n}b')
    return r, v, a[0] == b[0] and r[0] != a[0]

print('== Problem 1')
r, f, c = add(0b1011101, 0b110110, 8)          # 1011.101 + 110.11 aligned to 3 digits: x8
print(f'a) carries   {f}  {r} -> {int(r, 2) // 8 :b}.{r[-3:]} = {int(r, 2) / 8}  ({0b1011101 / 8} + {0b110110 / 8})')
r, f = subtract(197, 110, 8)
print(f'b) borrows   {f}  11000101 - 01101110 = {r} = {int(r, 2)}')
r1, f1, c = add(85, 63, 8); r2, f2 = subtract(int(r1, 2), 54, 8)
print(f'c) carries   {f1}  85 + 63 = {r1} = {int(r1, 2)};  borrows   {f2}  - 54 = {r2} = {int(r2, 2)}')
print('== Problem 2')
parts = [0b10111 << i if 0b1101 >> i & 1 else 0 for i in range(4)]
print('a) partials', [format(p, 'b') for p in parts], '->', format(sum(parts), 'b'), '=', sum(parts), '=', 23 * 13)
rem, quot, steps = 0, '', []
for bit in '1101101':
    rem = rem * 2 + int(bit)
    quot += '1' if rem >= 11 else '0'
    steps.append(f'{rem:b}{"-1011" if rem >= 11 else ""}')
    rem -= 11 if rem >= 11 else 0
print(f'b) steps {steps}  quotient {quot} = {int(quot, 2)}, rem. {rem:b} = {rem}  ({109 // 11}, {109 % 11})')
print('== Problem 3')
sm = lambda x, n=8: ('1' if x < 0 else '0') + format(abs(x), f'0{n - 1}b')
print(f'a) +45 SM {sm(45)} C1 {c1(45)};  -45 SM {sm(-45)} C1 {c1(-45)}')
s = '10101100'
print(f'b) {s}: SM = {-int(s[1:], 2)}, C1 = {val_c1(s)}, unsigned = {int(s, 2)}')
print(f'c) 8 bits: SM and C1 from {-(2 ** 7 - 1)} to {2 ** 7 - 1}; unsigned from 0 to {2 ** 8 - 1}')
print(f'd) -45 + 20 -> 45 - 20 = 25, sign of 45 -> {sm(-25)} = -25;  -45 + -20 -> 65 with sign - -> {sm(-65)} = -65')
print('== Problem 4: ((-37(10 + (-2A(16)) + 15(8)')
a, b, c = -37, -int('2A', 16), int('15', 8)
print(f'data:  37 = {c1(37)}, 2A(16 = {int("2A", 16)} = {c1(42)}, 15(8 = {c} = {c1(c)}')
r1, v1, d1 = add_c1(c1(a), c1(b)); r2, v2, d2 = add_c1(r1, c1(c))
print(f'1) {c1(a)} + {c1(b)} -> {r1} (wrap {v1}) = {val_c1(r1)}  overflow {d1}')
print(f'2) {r1} + {c1(c)} -> {r2} (wrap {v2}) = {val_c1(r2)}  overflow {d2}')
print(f'decimal check: {a} + {b} + {c} = {a + b + c}')
x, y, z = -70, -int('3C', 16), int('12', 8)                # the «Try it yourself»
r1, v1, d1 = add_c1(c1(x), c1(y)); r2, v2, d2 = add_c1(r1, c1(z))
print(f'try it: {c1(x)} + {c1(y)} -> {r1} (wrap {v1}) overflow {d1}; + {c1(z)} -> {r2} = {val_c1(r2)}; real {x + y + z}')
== Problem 1
a) carries   11111...  10010011 -> 10010.011 = 18.375  (11.625 + 6.75)
b) borrows   111111..  11000101 - 01101110 = 01010111 = 87
c) carries   1111111.  85 + 63 = 10010100 = 148;  borrows   111111..  - 54 = 01011110 = 94
== Problem 2
a) partials ['10111', '0', '1011100', '10111000'] -> 100101011 = 299 = 299
b) steps ['1', '11', '110', '1101-1011', '101', '1010', '10101-1011']  quotient 0001001 = 9, rem. 1010 = 10  (9, 10)
== Problem 3
a) +45 SM 00101101 C1 00101101;  -45 SM 10101101 C1 11010010
b) 10101100: SM = -44, C1 = -83, unsigned = 172
c) 8 bits: SM and C1 from -127 to 127; unsigned from 0 to 255
d) -45 + 20 -> 45 - 20 = 25, sign of 45 -> 10011001 = -25;  -45 + -20 -> 65 with sign - -> 11000001 = -65
== Problem 4: ((-37(10 + (-2A(16)) + 15(8)
data:  37 = 00100101, 2A(16 = 42 = 00101010, 15(8 = 13 = 00001101
1) 11011010 + 11010101 -> 10110000 (wrap 1) = -79  overflow False
2) 10110000 + 00001101 -> 10111101 (wrap 0) = -66  overflow False
decimal check: -37 + -42 + 13 = -66
try it: 10111001 + 11000011 -> 01111101 (wrap 1) overflow True; + 00001010 -> 10000111 = -120; real -120

What to look at:

  1. In problem 1, the rows of carries and borrows are the same ones we wrote by hand: compare them column by column.
  2. In problem 2, the steps list shows what is brought down at each step; the ones with -1011 are the ones that give a 1 in the quotient.
  3. In problem 3, the same code 10101100 gives three different values depending on the format.
  4. In problem 4, the first addition gives wrap 1 and the second wrap 0, and neither overflows; in the «Try it yourself», the first addition overflows even though the final result matches the decimal one.

Summary

  • Adding and subtracting in binary is column by column, with the row of carries or borrows written on top and the points lined up.
  • In a chain you work two at a time, keeping the order of the statement.
  • Multiplying is adding shifted copies of the multiplicand; dividing is bringing down bits and writing a 1 or a 0 at each step.
  • The same code means different things unsigned, in sign-magnitude and in C1: the format is part of the data.
  • In C1 you subtract by adding the complement and the end carry comes back on the right; overflow shows in the sign.
  • Every calculation is checked in decimal, and divisions with quotient × divisor + remainder.

Checklist: have you practised it?

  • Have you done the four problems on paper before reading each «Step by step»?
  • Can you add two numbers with a binary point by lining up the points and filling with zeros?
  • Can you do a subtraction with chained borrows without losing any?
  • Do you write the zeros of the quotient in a division?
  • Can you read the same code unsigned, in sign-magnitude and in C1?
  • Have you tried the «Try it yourself» without looking at the theory?

If you answered yes to almost everything, you are ready for the binary arithmetic exercises, where I no longer give you the solution next to the statement. See you there!

Next step: exercisesBinary arithmetic — solved exercises with the lift scale, trips in binary, a 6-bit panel and a practice quiz (and the overflow I missed by looking at the carry)

Did this article help you? Here is how you can support SergioLearns →