# Japanese abacus and binary arithmetic

Soroban or counting tray is a vertical abacus with 5 beads that is divided into two rows. Higher row has one bead and lower row has 4.

Every column in a soroban signifies a digit. The lone higher bead has a value of 5 and lower beads each has a value of 1. The value of the column is decided by the amount of beads touching the middle bar that divides the Soroban.

Mastering the four basic arithmetic operations on Soroban is said to boost your calculation skill. I think there is a very good reason for this. A physical tool allows you to memorize the arithmetic operations into a mantra that you can apply without thinking about it.

The use of Soroban for addition relies on the complement of 5 and 10.

``````1+4 = 2+3 = 3+2 = 4+1 = 5
````1+9 = 2+8 = 3+7 = 4+6 = 5+5 = 6+4 = 7+3 = 8+2 = 9+1 = 10````

So say you have to add A to B. If complement of A is higher than B, then there is no carry. Otherwise there is a carry.

``````A+B < 10           when B  < (10-A)
````A+B = +10+A-(10-B) when B >= (10-A)````

Likewise at subtraction you can also use the complement to ease computation.

``````A-B >= 0           when B <= A
````A-B = -10+A+(10-B) when B >  A````

Next consider you have to calculate this:

``````42933 + 13749
````  * *     * *````

The stars mark the calculations that produce complements. You can rewrite the number to this:

``````42933 + 13040
````  3 1    1 1````

This is a shorthand for `42933 + 13040 - 300 - 1 + 1000 + 10`. When you carry out the simpler calculations you end up with no complementary number combinations and you can just sum every number together like this:

``42632 + 14050 = 56682``

The result is the same as of the original calculation.

You can do the same with the subtraction:

``````42933 - 26872
`````` * *     * *
``````
``````42933 - 20802
`````` 4 3    1 1
``````
````46963 - 30902 = 16061````

The logic is similar, but the complementary numbers are added instead of subtracted, and the carry is subtracted instead of added. We mark the numbers that are smaller than the numbers on the another side.

What does happen with adjacent complementaries?

``````4693 + 5379
``````!!**     **
``````
``````4693 + 5300
````!!31    11````

You can see that the rules work fairly well because we end up subtracting. I intentionally chose a corner case where we get an additional complementary.

``````4693 + 5000
``````!631   1 1
``````
``````4693 + 0000
``````4631  1  1
``````
``````0093 + 0000
``````  31  1  1
``````
````0062 +10010 = 10072````

Digits that add up to 9 can carry. This may seem like a problem to use of this algorithm, but they clear down to zero if they interfere with the calculation.

In the subtractive case the carrying digits are the same, which makes them easy to recognize.

These rules are practically the same as with ordinary arithmetic that you may have been taught so there should be nothing new for you in this drill.

How do these rules manifest in a soroban? Well practically they should be simpler because the soroban represents digits using fewer digits.

This is how the digits would manifest in a soroban:

`````` 55    5 55
````4143 +  324````

Now lets try the same algorithm to calculate the sum and mark the carrying digits:

``````!!*!     *
``````0550   5055
``````4143 + 0324
````!!**     **````

I found this to be a bit tricky because the higher column behaves differently than the lower. In other hand it is much simpler.

``````!!*!     *
``````0550   5055
``````4143 + 0324
``````!!**     **
``````
``````!! !    1
``````0500   5005
``````4143 + 0324
````!!**     **````

The large carry makes it look as if the whole number just collapsed.

``````   !
``````0000   0005
``````0043 + 0024
``````  **  1  **
``````
``````
``````0000   0050
``````0043 + 0010
``````  31  1
``````
``````0000   0050   00050
````0012 +10010 = 10022 = 10072````

Here we are doing rather clumsy calculations on the paper, but lets do it again as if we were actually using a soroban to calculate the sum:

``````18233 + 99777
``````
``````005000   55555
``````013233 + 44222
``````
``````055000    5555
``````013233 + 44222
``````
`````` 05000    5555
``````103233 +  4222
``````
`````` 05000     555
``````112233 +   222
``````
`````` 05500      55
``````112233 +   222
``````
`````` 05500      55
``````112433 +    22
``````
`````` 05550       5
``````112433 +    22
``````
`````` 05000       5
``````113003 +     2
``````
`````` 05005
``````113003 +     2
``````
`````` 05000
````113010 = 118010````

It is a long sequence of events but it really did not take much time at all. Literally everybody knows how to add and subtract together small numbers and the use of Soroban exploits that fact.

Knowing how Soroban adds numbers together also helps you in understanding how computers add numbers together. Here's the same computation done using the binary numbers:

`````` !  !!    *!*  !!!! !  *
````00  0100 0111  0011 1001 + 01  1000 0101  1100 0001````

The information about the carry and hazard of carrying further already tells us everything we need to know about the result.

``01  1100 1100  1111 1010 = 118010``

The logic of addition reduces into small pieces that can be implemented in handful of logic gates.

``````0+0+0 => 0,  0+1+0 => 1,  1+0+0 => 1,  1+1+0 => 2
````0+0+1 => 1,  0+1+1 => 2,  1+0+1 => 2,  1+1+1 => 1+2````

The additive circuit can also double as subtraction circuit if we use two's complement to represent negative numbers.

## Two's complement

When two's complement is used with binary numbers, the most significant digit is interpreted as if it extended infinitely to the left. These four numbers are all the same number:

``````11110101 = -11
``````111110101 = -11
``````1111110101 = -11
````11111110101 = -11````

To get the negated number value, you flip the bits and add one.

``~11110101+1 = 00001010+1 = 11``

Therefore our algorithm for adding numbers together remain the same, yet we have obtained a way to subtract numbers and represent a negative sign. We can verify this by observing that the negative and positive number cancels each other out.

``````!!!!!!!*
````11110101 + 00001011 = 0````

So Soroban's arithmetic resembles binary arithmetic because the rules to memoize are reduced in count. Can we use this idea of complementary numbers with Soroban? We can try to derive the rules.

``````42933 - 26872 = 16061
``````
``````00500   05550   05050
````42433 - 21322 = 11011````

So what is the negation of `26872` in soroban representation? Observing that the complementary representation must cancel out, we should produce a complementary number with an expectation that the number cancels out.

The complementary number would be:

``````550005
````423122+1````

It is as if we subtracted every digit from `9`, then added `1`.

We can see that this number cancels out with the positive number during negation:

``````!!!!!!
``````550005   005550
``````423123 + 021322 = 0
````!!!!!*````

Now we can try whether the addition with this complementary produces the result that we wanted:

``````000500   550005   005050
``````042433 + 423123 = 011011
``````
``````500500    50005
``````442433 +  23123
``````
``````550500     0005
``````442433 +  23123
``````
``````000500      005
``````012433 +   3123
``````
``````005500       05
``````010433 +    123
``````
``````005000        5
``````011033 +     23
``````
``````005050        5
``````011003 +      3
``````
``````005055
``````011003 +      3
``````
``````005050
````011011````

Therefore complementary number notation does work with a soroban, which means we only need to know how to invert the orientation of the beads and add one to represent negative numbers.

## Multiplication

The multiplication of numbers together requires a multiplication table. The multiplication table for decimals goes:

``````1
``````2  4
``````3  6  9
``````4  8 12 16
``````5 10 15 20 25
``````6 12 18 24 30 36
``````7 14 21 28 35 42 49
``````8 16 24 32 40 48 56 64
````9 18 27 36 45 54 63 72 81````

Low number of digits makes the multiplication easier. For reference, here's the multiplication table for the binary:

``````0 0
````0 1````

That is a ridiculous multiplication table. So how does multiplication of two numbers proceed in binary?

``````00  0100 0111  0011 1001 * 01  1000 0101  1100 0001
``````
``````                                 011000010111000001
``````00000000000000000000100011100111001
``````000000000000000000000100011100111001
``````00000000000000000000000000100011100111001
``````0000000000000000000000000000100011100111001
``````00000000000000000000000000000100011100111001
``````000000000000000000000000000000100011100111001
``````000000000000000000000000000000000000100011100111001
``````
``````000000000000000000001101010110101011
``````000000000000000000000 000   00   00
``````00000000000000000000000000100011100111001
``````0000000000000000000000000000100011100111001
``````00000000000000000000000000000100011100111001
``````000000000000000000000000000000100011100111001
``````000000000000000000000000000000000000100011100111001
``````
``````00000000000000000000110101111110010011001
``````00000000000000000000000000 000   00   00
``````0000000000000000000000000000100011100111001
``````00000000000000000000000000000100011100111001
``````000000000000000000000000000000100011100111001
``````000000000000000000000000000000000000100011100111001
``````
``````0000000000000000000011011000011100110011101
``````0000000000000000000000000000 000   00   00
``````00000000000000000000000000000100011100111001
``````000000000000000000000000000000100011100111001
``````000000000000000000000000000000000000100011100111001
``````
``````00000000000000000000110110001011101001110011
``````00000000000000000000000000000 000   00   00
``````000000000000000000000000000000100011100111001
``````000000000000000000000000000000000000100011100111001
``````
``````000000000000000000001101100011011110000011111
``````000000000000000000000000000000 000   00   00
``````000000000000000000000000000000000000100011100111001
``````
``````000000000000000000001101100011011110100100011111001
``````000000000000000000000000000000000000 000   00   00
``````
````0110 1100  0110 1111  0100 1000  1111 1001````

What is the multiplication table for the Soroban?

``````0
``````1
``````0  0
``````2  4
``````0  5  5
``````3  1  4
``````0  5  0  5
``````4  3 12 11
``````5  0  5  0  5
````0 10 10 20 20````

We have 10 numbers to memorize and they all follow some blatantly obvious patterns. The five is ridiculously simple, whereas four is following a similar pattern that `9` follows in decimal. Two-four and Three-one-four are some trivial number sequences for the two and three.

The multiplication follows the exact same rules as multiplication with decimals or binary numbers. So I am not going to show an example of that using Soroban notation. Note that the multiplication also works with the complement numbers.

## Division

The division is the most difficult from the four steps, but in binary it's not that difficult when we use the methods that we have used so far to the other operations.

``000100011100111001 / 01110, the -01110 is 10010``

The strategy usually used for division is to transfer the number being divided into a remainder. Then we start subtracting multiples of the dividing number until we end up with a number that is smaller than the divider.

``````000000010000000000
``````000100011100111001
``````11110010
``````
``````000000010010000000
``````000000111100111001
``````11111110010
``````
``````000000010100000000
``````000000100000111001
``````11111110010
``````
``````000000010100010000
``````000000000100111001
``````11111111110010
``````
``````000000010100010100
``````000000000001011001
``````1111111111110010
``````
``````000000010100010110
``````000000000000100001
``````11111111111110010
``````
``````000000010100010110
``````000000000000000101
``````111111111111110010
``````
````000100011100111001 / 01110 = 010100010110 + 0101 / 01110````

How to do this with a Soroban?

``````18233 / 14 = 1302 + 5 / 14
``````
``````05000
``````13233 % 14
``````
``````00000
``````04233 % 14 + 1000
``````
``````00000
``````04233 % 14 + 1000
``````
``````00500
``````02333 % 14 + 1100
``````
``````00000
``````01433 % 14 + 1200
``````
``````00000
``````00033 % 14 + 1300
``````
``````00005
``````00014 % 14 + 1301
``````
``````00005
````00000 % 14 + 1302````

The method of long division can be used without a problem. It is essentially same as used with the binary numbers.

The long division with Soroban can be sped up by calculating a multiplication table for the divider. This is achieved by adding the number to itself five times.

## IEEE Floating point soroban

You could create an abacus with pairs of three beads in it, used and organized similar to the Soroban. This "hexadecimal" Soroban could be directly used for representing floating point arithmetic.

Here's a floating point soroban that represents infinity:

``````+INF
``````----------------------------------
``````| | |  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````- * *  * * * *  * * * *  * * * * |
``````* * *  | | | |  | | | |  | | | | |
``````|--------------------------------|
``````| * *  | | | |  | | | |  | | | | |
``````- * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````| | |  * * * *  * * * *  * * * * |
````----------------------------------````

If the significand is not zero, this table would be NaN.

``````+NaN(0h.4)
``````----------------------------------
``````| | |  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````- * *  | * * *  * * * *  * * * * |
``````* * *  * | | |  | | | |  | | | | |
``````|--------------------------------|
``````| * *  | | | |  | | | |  | | | | |
``````- * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````| | |  * * * *  * * * *  * * * * |
````----------------------------------````

Now you can see yourself that floating point format uses a lot of states for representing NaN.

Exponent zero represent denormal numbers. They are numbers that start with zero. The smallest representable number would be shown like this:

``````+0h0.000000000001*2^(-126)
``````----------------------------------
``````| * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````- * *  * * * *  * * * *  * * * * |
``````* | |  | | | |  | | | |  | | | | |
``````|--------------------------------|
``````| | |  | | | |  | | | |  | | | * |
``````- * *  * * * *  * * * *  * * * | |
``````| * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
````----------------------------------````

If the exponent is nonzero, we get normalized values.

``````+0h1.000000000000*2^(-126)
``````----------------------------------
``````| * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````- * |  * * * *  * * * *  * * * * |
``````* | *  | | | |  | | | |  | | | | |
``````|--------------------------------|
``````| | |  | | | |  | | | |  | | | | |
``````- * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
````----------------------------------````

This is the quantity of 1:

``````+0h1.000000000000e0*2^(0)
``````----------------------------------
``````| * |  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````- | *  * * * *  * * * *  * * * * |
``````* * *  | | | |  | | | |  | | | | |
``````|--------------------------------|
``````| * *  | | | |  | | | |  | | | | |
``````- * *  * * * *  * * * *  * * * * |
``````| * *  * * * *  * * * *  * * * * |
``````| | |  * * * *  * * * *  * * * * |
````----------------------------------````

What could you do with this? You could demonstrate how the value underflows if the exponent differs by more than 48 steps. You can also explain why some decimal fractions do not have an exact representation in floating point values. Plus you can explain why the floating point rounding error accumulates or how the error magnifies when values are subtracted from each other.

The computation would involve shifting the beads into the right orientation. This would involve left and right shifts.

``````1011 0000
``````---------
``````
``````  *
``````  *
``````---------
``````  *
``````  *
````  *````

Right shift: If the slot has at least two beads, leave odd number of beads. If the slot has odd number of beads, leave at least two beads to the next slot.

``````0101 1000
``````---------
``````
``````    *
``````  * *
``````---------
````  *````

Left shift: If the slot has odd number of beads, leave more than two beads. If the slot has at least two beads, leave odd number of beads to the previous slot.

``````0001 0110
``````---------
``````
``````
``````    *
``````---------
``````  * *
````    *````

Maybe there will be more ideas later.

## Conclusion

Use of a Soroban in training could help to train mechanized rules for basic arithmetic and dramatically speed up the performance of calculating arithmetic by memory.

The speed-up could be used for teaching more of numerical methods for solving problems that would be otherwise infeasible due to lot of computation required to solve the problem.

The knowledge gained from operating with a Soroban can translate to the understanding of computers and computer arithmetic.