Computers do arithmetic in the binary number system.  1  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. Consider two two-digit numbers, $x = 67$ and $y = 79\text{.}$ Adding these by hand on paper would look something like:

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.2.3), we describe addition of two decimal integers in Algorithm 3.1.1.

Note that:

1. 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.

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

3. 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.

4. 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.

# Subsection3.1.1Exercises

##### 1

How many bits are required to store a single decimal digit?

##### 2

Using your code from Exercise 3.1.1.1 invent a code for storing two decimal digits in 32 bits. Using this code, does binary addition produce the correct results?

Solution
##### 3

Develop an algorithm for adding fixed-width integers in the binary number system.

Hint Solution
##### 4

Develop an algorithm for adding fixed-width integers in the hexadecimal number system.

Solution
##### 5

Develop an algorithm for subtracting fixed-width integers in the binary number system.

Hint Solution
##### 6

Develop an algorithm for adding fixed-width integers in the hexadecimal number system.

Solution