Episode 3.02 – Tens Complement Arithmetic

Welcome to the Geek Author series on Computer Organization and Design Fundamentals. I’m David Tarnoff, and in this series we are working our way through the topics of Computer Organization, Computer Architecture, Digital Design, and Embedded System Design. If you’re interested in the inner workings of a computer, then you’re in the right place. The only background you’ll need for this series is an understanding of integer math, and if possible, a little experience with a programming language such as Java.

If you listened to our last episode, you probably came to the realization that subtraction in binary can be challenging. It isn’t much easier at the electronics level with all of those borrows that could propogate from column to column all the way to the most significant bit. So in this episode, we’re going to introduce a decimal representation for negative numbers that allows us to use addition to perform a subtraction. This new decimal representation has the added benefit of representing negative numbers without requiring a minus sign. It will allow for consistent arithmetic operations regardless of relative magnitudes or signs. It will also lay the foundation for identical benefits in binary when we shift from base-10 to base-2.

In 1645, Blaise Pascal presented his calculator, the Pascaline, to the public. By today’s standards, the little device was not complex, it’s operation similar to that of an old school automotive odometer. Like the odometer, each decimal place of Pascal’s calculating machine was “stored” using a rotating wheel of which only a single numeral could be seen through a small window. The numerals 0 through 9 were written in order around the perimiter of the wheel so that as the wheel turned a single digit could be seen that represented the magnitude for that power of ten position. In today’s vernacular, this array of wheels capable of storing the digits of a decimal number is referred to as an accumulator.

Below each window of the accumulator there was a dial that the user could turn using a stylus. Like the rotating wheels, each of these dials also had ten decimal numerals spaced evenly around its perimeter. As the user turned one of the dials with the stylus, the wheel above that dial would turn at the same rate making it so that the number of places turned by the dial was added to the digit in the window above it.

The magic of this machine was that it handled the carries that would propogate from one column to the next. If while rotating a dial, a digit displayed in one of the windows of the accumulator passed from 9 to 0, the next highest wheel would automically increment by one position passing along a decimal carry – just like an odometer.

By starting with all the numbers of the accumulator cleared to zero, the first number to be added was entered by rotating the dial for each decimal place so that the correct value appeared in each window. Then, the number to be added to the accumulator could be entered by rotating the dials again for each decimal place. The result, as long as it fit in the number of available digits of the accumulator, would be displayed through the windows. By the way, it will be important to remember the phrase, “as long as it fit in the number of available digits.” This will be vital when we start performing subtractions.

Now that we have a device that adds, we can also multiply through repeated additions. In other words, if we want to multiply X times Y, we simply add X Y times. This could be done on the Pascaline by adding first the ones digit of X Y times, then the tens digit of X Y times, then the hundreds digit of X Y times, and so on. There are two more basic arithmetic functions, however, that would be nice to have: subtraction and division. Unfortunately, the dials of the Pascaline did not turn backwards, nor were the mechanisms of the accumulator capable of borrowing from higher order digits. Therefore, before we move to subtraction, we need to discuss a different form of numeric representation.

In decimal arithmetic, every number has an additive inverse, in other words, a value which when added to the original number results in a zero. For example, 3 and -3 are additive inverses of one another because 3 + (-3) = 0.

When, as with the Pascaline, we have a fixed number of decimal places, there is a second way to create an additive inverse. If we limit ourselves to a single digit and ignore any carry generated out of the ones position, it turns out that there is a positive digit we can add to 3 that will also give us 0: 7. Seven? Yes, 7. When we add 3 to 7, we get 10, but by restricting ourselves to the single digit of the ones place and ignoring the carry, we get 3 plus 7 equals 0.

Well, that just seems weird. But does it? We can exend this to two digits. What is the additive inverse of 53? Well, it’s -53. But if we limit ourselves to two digits, the additive inverse of 53 is 47. 53 + 47 equals 100, but remember that we’re limiting ourselves to two digits and discarding carries out of the most significant place. That means the one that was carried out of the tens place is discarded and we get a result of zero-zero.

Let’s do this one more time. If we limit ourselves to four digits, then what is the additive inverse of 253? To get a result of 0000 with a discarded carry, we need to add 9747 to 253. As a side note, we need to recognize that 253 in four decimal digits is actually 0253. In a moment, we will see the importance of this.

There is an algorithm that allows us to generate the additive inverse. We work out the ones place of our inverse by subtracting the ones place of the original value by 10. For our example, 3, which is the ones place out of 253, is taken out of 10, which gives us 7, the ones place of our inverse. For the remaining positions, we subtract each digit from 9. In the tens place, 9 minus 5 gives us 4. In the hundreds place, 2 from 9 equals 7. Our original value had 0 for its thousands component, so to determine the thousands place of our inverse, we subtract 0 from 9 to get 9. That gives us our inverse of 9747.

This algorithm works for all cases except when the digit in the ones place, is 0. Therefore, the true algorithm is to subtract each digit from 9 to get the new decimal places for the inverse, then add one to the result. This numeric representation, referred to as tens complement, requires us to fix the number of decimal places.

Let’s say we want to compute the 3-digit tens complement of 42, or more precisely, 042. Zero taken from 9 is 9, 4 taken from 9 is 5, and 2 taken from 9 is 7. That gives us 957. Add 1 to this value, and we see that the 3-digit additive inverse of 042 is 958. We can verify this by adding 42 to 958, the result of which is 1000. But remember, we are limiting ourselves to three fixed digits, so the carry into the thousands place is discarded and we get 000.

This tens complement thing seems to work for additive complements, but will the additive complement work like a negative number? Well, let’s add 088 to our additive complement of 042. 088 + 958 = 1046. If we throw away the carry that went into the thousands place, we get 46, which just so happens to be the result from the operation of 88 minus 42.

What happens, however, if we add a value smaller than 42 to negative 42, say 23? Well, adding 23 to 958 gives us 981. Well that didn’t work. Or did it? The result should have been negative, so maybe 981 is simply the additive inverse of the magnitude of the answer. Let’s compute the tens complement additive inverse of 981. First, subtract each digit from 9 to get 018. Adding one gives us 019. Well, I’ll be darned, 23 minus 42 is -19.

So how do we distinguish positive numbers from negative numbers in tens complement representation? It turns out that we need to sacrifice the left most or most significant digit as the sign representation. If the most significant digit is a 0, the numeric result is positive. If the most significant digit is a 9, the numeric result is negative. If our addition result starts with a 9, then we need to compute the tens complement additive inverse to determine the magnitude of this negative number. If the most significant digit of our result is any digit other than 0 or 9, then our accumulator has too few digits to represent the result. This is referred to as an overflow.

Pascal took advantage of the method of tens complement. On the Pascaline, there was a bar that slid up and down in front of the windows of the accumulator. Sliding the bar up revealed the nines complement of the accumulator. Adding a one to this value would result in the tens complement. And as for division, that could be performed by counting the number of repeated subtractions needed before the accumulator contained a value less than the divisor. This final value in the accumulator would be the remainder.

As recently as the mid-1970’s, mechanical calculators like the Pascaline continued to be manufactured. Devices sold under brand names such as Addiator and Baby Calculator used sliders instead of dials to add decimal values to the accumulator. And just like the Pascaline, these devices were able to subtract and divide through the use of the methods of tens complement.

In our next episode, we will show how modern computers use a binary version of tens complement called twos complement to represent negative numbers. For transcripts, links, or other podcast notes, please check us out at intermation.com where you will also find links to our Instagram, Twitter, Facebook, and Pinterest pages. Until then remember that while the scope of what makes a computer is immense, it’s all just ones and zeros.