Computers do arithmetic in the binary number system. The operations are really quite easy to understand if you recall all the details of performing arithmetic in the decimal number system by hand. Since most people do addition on a calculator these days, let us review all the steps required when doing it by hand. This discussion will be followed by exercises asking you to develop the algorithms to perform addition and subtraction in binary and in hexadecimal.

Consider two two-digit numbers, $$x = 67$$ and $$y = 79\text{.}$$ Adding these by hand on paper would look something like:

 $$1$$ $$1$$ $$\longleftarrow$$ carries  $$67$$ $$\longleftarrow$$ $$x$$ $$+$$ $$79$$ $$\longleftarrow$$ $$y$$  $$46$$ $$\longleftarrow$$ sum

We start by working from the right, adding the two decimal digits in the ones place. 7 + 9 exceeds 10 by 6. We show this by placing a 6 in the ones place in the sum and carrying a 1 to the tens place. Next we add the three decimal digits in the tens place, 1 (the carry into the tens place from the ones place) + 6 + 7. The sum of these three digits exceeds 10 by 4, which we show by placing a 4 in the tens place in the sum and recording the fact that there is an ultimate carry of one. Recall that we had decided to use only two digits, so there is no hundreds place. Using the notation of Equation (2.3.3), we describe addition of two decimal integers in Algorithm 3.1.1.

Note that:

• Algorithm 3.1.1 works because we use a positional notation when writing numbers—a digit one place to the left counts ten times more.

• Carry from the current position one place to the left is always 0 or 1.

• The reason we use 10 in the / and % operations is that there are exactly ten digits in the decimal number system: 0, 1, 2,…, 9.

• Since we are working in an N-digit system, we must restrict our result to N digits. The final carry, $$carry_{N}\text{,}$$ is either $$0$$ or $$1$$ and is part of the result, along with the N-digit sum.

Let us now turn to the subtraction operation. As you recall from subtraction in the decimal number system, you must sometimes borrow from the next higher-order digit in the minuend. This is shown in Algorithm 3.1.2.

This algorithm is not nearly as complicated as it first looks. When you do subtraction on paper, you do all these things automatically.

When adding and subtracting in the decimal system you are used to dealing with carry and borrow “in your head,” but that may not be as intuitive for you in the binary and hexadecimal systems. You should memorize Table 3.1.3, which will help you to work in these other number systems, which you are asked to do in Exercises 3.2.1–3.2.6 following this section.