5 ms·
The complements are also really useful for subtracting numbers. Not only in the binary system, but also in the decimal system. They allow you to subtract by add
by Lukas_Skywalker 3y ago
The complements are also really useful for subtracting numbers. Not only in the binary system, but also in the decimal system. They allow you to subtract by adding numbers.
Let's say you need to calculate
8467
-4583
The ten's complement of 4583 is 5417 (the complement is the remainder to the next power of ten, in that case to 10000). A cool property of the ten's complement in the decimal system is that the subtraction above can be rewritten as
8467
+5417
-10000
Which results in
13884
-10000
------
3884
This is much easier to calculate than the direct subtraction. It exploits the fact that calculating -x is equal to +(10000 - x) - 10000.
The same works for binary and the two's complement, where it is even easier, because calculating the two's complement is equal to inverting all digits and adding 1.
It works for any number system by using the complement of the system's base.
- tuukkah 3y ago> Let's say you need to calculate 8467 - 4583 > The ten's complement of 4583 is 5417 It looks like here you are calculating 9999 - 4583 = 5416 (an easy subtraction), then adding one. > (the complement is the remainder to the next power of ten, in that case to 10000) Is remainder a correct term here? It's confusing because if you divide 4583/10000, the remainder is 4583. Modulo of -4583 by 10000 is 5417 though. > This is much easier to calculate than the direct subtraction. This is not clear to me. Is it because when you calculate 9999-yyyy, you never need to borrow? Or when you calculate 10000-yyyy, you always make a simple borrow? In any case, I feel this still takes some practise because what was a single subtraction becomes two or more steps. Direct subtraction with paper and pencil isn't much more difficult than direct addition anyway. If you want to do it in your head, I'd prefer a single-step method instead of having to remember the intermediate results.
- IIAOPSW 3y agoImagine you're doing your math on a car odometer that only goes up to 4 digits. When you reach 10k it wraps around. For any X it is easy to find the complement ~X because you can just map the digits independently (0,1,2,3,4 -> 9,8,7,6,5 and vice versa) then adding 1. By construction each digit in X + (~X-1) will be 9, then adding the 1 back in brings it to 10000, which in odometer math wraps around to 0. So ~X is the additive inverse of X meaning that adding ~X always brings you to the same number as subtracting -X. Or think of a clock. Subtracting X hours is always the same as adding (12-X) hours. Or any circle, turning counterclockwise X degrees is always the same as turning clockwise (360-X) degrees. The trick is being able to take the compliment digit-wise in order to replace a subtraction with an addition. In binary this is just flipping 0 to 1 and vice versa. In base 10 you just need to remember 5 pairs of digits to switch out with each other.
- skeeter2020 3y agothey've actually combined some of the steps which might make it easier to understand. It's important that we keep in mind we have 4 digits. 8467 - 4583 = 8467 + 5416 (9's complement of 4583) = 13883 + 1 (10's complement) = 13884 - 10000 = 3884 >> Direct subtraction with paper and pencil isn't much more difficult than direct addition anyway. If you want to do it in your head, I'd prefer a single-step method instead of having to remember the intermediate results. This isn't really a technique for humans, but computers. You can implement add with carry pretty easily with a bunch of logic gates, but what about subtraction where you need to jump across several columns to borrow, keeping track across the entire minuend? Computers don't typically have those little tick marks beside each digit, or scrap paper. It is easy to set a "subtracting" flag though, and invert the subtrahend (1's compliment), and then feed the first carry-in of the ALU with the same signal as the subtract flag (add 1 for 2's complement). You could come up with your own complete subtraction logic, but it's definitely easier to use the same core addition with a tiny bit of extra to handle subtraction and then use a method like this.
- venusenvy47 3y agoDid you calculate the 9's complement of 4583 in your head? I see that 5416 has individual digits that line up with 4583 to each equal 9, but I'm not clear if this is a coincidence that happens to work for this number, or if this is the way to calculate an arbitrary N's complement value.
- Lukas_Skywalker 3y agoThat is exactly the 9‘s complement. The digits add up to 9.
- venusenvy47 3y agoI thought there was more to the calculation of N's complement. Something about adding one to the result. It doesn't seem like something you can easily perform using inspection.
- schwartzworld 3y agoIt seems like a neat trick, but I don't know if I'd call it much easier. It took me like a second to do the math in my head.
- Lukas_Skywalker 3y agoIt depends on the numbers. But you don't have to borrow from the next digit the whole time and keep track of that.
- ommpto 3y agoHow so, you still have to keep track of the overrun, but this time even twice. First when doing 10000 - 4583 and second when adding? I don't see how that would be easier?
- hammock 3y agoThis explanation has made me realize i can’t remember the last time I tried to substract two four digits numbers without a calculator. I’m not even sure I know how
- ziml77 3y agoIn case you're serious, just go right to left, digit by digit. 7 - 3 = 4: 8 4 6 7 - 4 5 8 3 ------------- 4 6 is less than 8, so borrow from the next digit. 16 - 8 = 8: 8 3 16 7 - 4 5 8 3 ------------- 8 4 3 is less than 5 so borrow from the next digit. 13 - 5 = 8: 7 13 16 7 - 4 5 8 3 ------------- 8 8 4 7 - 4 = 3: 7 13 16 7 - 4 5 8 3 ------------- 3 8 8 4 And there's the answer!
- hammock 3y agoI guess that’s how I would attempt it if I had to- reverse of addition. Just can’t remember when I have had to. Funny how addition and subtraction are unequally distributed in life
- masswerk 3y agoInteresting cultural differences here: the way I learned it, you increment the next digit of the subtrahend (the lower number) on digit underflow, rather than borrowing from the next digit in the minuend. (So it's rather a carry than borrowing.) Anyways, explicit borrow does for a nice explanation.
- ziml77 3y agoThat is interesting! I could see that make learning it easier since it's closer to addition if you carry instead of borrow. Though one advantage I see to the borrow is, since you're modifying the minuend, you have room to write the modified value of the digit directly above itself.
- masswerk 3y ago
- kps 3y agoI wish I'd been taught that as a child instead of the conventional method. I had no end of trouble with subtraction because we were taught ‘borrowing’, and being a moderately decent kid I always gave back what I borrowed.
- twoodfin 3y agoInterestingly, my 5yo insisted on the symmetry of calling the operation “carrying” in both directions.
- rahimnathwani 3y agoI don't like the term 'carrying' because it refers to a superficial aspect of the method (that you carry the number from one place to another) rather than the essence of the operation. The term 'borrowing' never made sense to me. Singapore Math uses the term 'regrouping' which makes a lot more sense: https://www.youtube.com/watch?v=mrDIf3YmNFc https://www.youtube.com/watch?v=mrDIf3YmNFc When you add, 15+6, you add 5+6 to get 11, and then you regroup the 11 ones into 1 ten and 1 one. When you subtract 21-6, you regroup the 2 tens into 1 ten and 10 ones. With the original 1 one, you now have 11 ones. Then you subtract the 6 from the 11 ones to get 5 as the last digit.
- hanche 3y ago> The same works for binary and the two's complement, where it is even easier, because calculating the two's complement is equal to inverting all digits and adding 1. It’s the same in base 10, if by inverting a digit x you mean replacing it by 9-x.
- userbinator 3y agoEarly mechanical calculators did something similar, although with 9s complement, to make it easy to subtract instead of add: https://upload.wikimedia.org/wikipedia/commons/5/5d/EarlyComptometerMachine.png https://upload.wikimedia.org/wikipedia/commons/5/5d/EarlyCom... (This is where the "accumulator" terminology for a 1-register machine came from too.)