Online Book Reader

Home Category

Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [53]

By Root 1590 0
adder. Also for a subtraction, you add 1 to the result of the addition by setting the CI (Carry In) input of the adder to 1. For an addition, the Ones' Complement circuit has no effect and the CI input is 0.

The SUB signal and the CO (Carry Out) output of the adder also go into an XOR gate that's used to light up the Overflow/Underflow lamp. If the SUB signal is 0 (which means an addition is being performed), the lightbulb will be lit if the CO output of the adder is 1. This means that the result of the addition is greater than 255.

If a subtraction is being performed and if the subtrahend (the B switches) is less than the minuend (the A switches), it's normal that the CO output from the adder is 1. This represents the 100000000 that must be subtracted in the final step. So the Overflow/Underflow lamp is lit only if the CO output from the adder is 0. This means that the subtrahend is greater than the minuend and the result is negative. The machine shown above isn't designed to display negative numbers.

You must surely be glad you asked, "But what about subtraction?"

I've been talking about negative numbers in this chapter, but I haven't yet indicated what negative binary numbers look like. You might assume that the traditional negative sign is used with binary just as it is in decimal. For example, –77 is written in binary as –1001101. You can certainly do that, but one of the goals in using binary numbers is to represent everything using 0s and 1s—even tiny symbols such as the negative sign.

Of course, you could simply use another bit for the negative sign. You could make that extra bit 1 for a negative number and 0 for a positive number, which would work, although it doesn't go quite far enough. There's another solution for representing negative numbers that also provides a hassle-free method for adding negative and positive numbers together. The drawback of this other method is that you must decide ahead of time how many digits are required for all the numbers you might encounter.

Let's think about this for a moment. The advantage of writing positive and negative numbers the way we normally do is that they can go on forever. We imagine 0 as the middle of an infinite stream of positive numbers going off in one direction and an infinite stream of negative numbers going off in another:

… –1,000,000 –999,999 … –3 –2 –1 0 1 2 3 … 999,999 1,000,000 …

But suppose we don't need an infinite number of numbers. Suppose we know at the outset that every number we come across will be within a particular range.

Let's look at a checking account, which is one place people sometimes see negative numbers. Let's assume that we never have as much as $500 in our checking account and that the bank has given us a no-bounce checking limit of $500. This means that the balance in our checking account is always a number somewhere between $499 and –$500. Let's also assume that we never deposit as much as $500, we never write a check for more than $500, and we deal only in dollars and don't care about cents.

This set of conditions means that the range of numbers we deal with in using our checking account include -500 through 499. That's a total of 1000 numbers. This restriction implies that we can use just three decimal digits and no negative sign to represent all the numbers we need. The trick is that we really don't need positive numbers ranging from 500 through 999. That's because we've already established that the maximum positive number we need is 499. So the three-digit numbers from 500 through 999 can actually represent negative numbers. Here's how it works:

To mean –500, we use 500.

To mean –499, we use 501.

To mean –498, we use 502.

(yada, yada, yada)

To mean –2, we use 998.

To mean –1, we use 999.

To mean 0, we use 000.

To mean 1, we use 001.

To mean 2, we use 002.

(yada, yada, yada)

To mean 497, we use 497.

To mean 498, we use 498.

To mean 499, we use 499.

In other words, every 3-digit number that begins with a 5, 6, 7, 8, or 9 is actually a negative number. Instead of writing the numbers like this:

–500 –499 –498

Return Main Page Previous Page Next Page

®Online Book Reader