Content translated by ChatGPT.
What is 1+1?
This question has troubled people for many years.
Some say one plus one equals 1, because two pieces of maltose candy stuck together are still one piece of maltose candy.
Some say one plus one equals 2, because if you double-click Calculator.exe, enter 1+1, the output is 2.
Some say…
…I have run out of things to make up.
So, good morning, afternoon, and evening to all the kids and grown-ups out there. Today, we are going to teach a computer how to perform addition and subtraction.
It is that simple.
Huh? It is not actually simple?
Zero: Let Us Start from Zero
Suppose your mom yells from the kitchen, “Kiddo, come eat! The food is getting coooold!” But you cannot rush to the dining table right away, because you are busy secretly using your mom’s Alipay to buy That Game 6! The only thing within reach is the mysterious switch for the dining-room chandelier. How can you convey a message to your mom?
Easy! Just control whether the light is on! We treat a lit bulb as True (or, more simply, 1) and an unlit bulb as False (or 0). That way, you can transmit information efficiently!
You then resolutely switch off the dining-room light.
Bad ending: your mom knows nothing about the arcane mysteries of computer organization. From her point of view…
This little brat is asking for a beating again. (grabs a clothes hanger)
What now? The end of the world is at hand! But do not panic: your mom still has ten seconds before she bursts through the door, which is more than enough time for us to begin the next section.
One: How Numbers Are Represented in a Computer
1. Binary
Now, imagine that you are holding an electrical wire. Yes, the very wire connected to the dining-room chandelier. Think back: how did you transmit a signal through the wire?
Like this: powered means 1, and unpowered means 0.
But that can represent only two states. The world is not simply black and white, so what if we need to represent four states?
Easy. Add another wire.
Now we can control the two wires to represent the four states 00, 01, 10, and 11. Let us number them!
00 represents 0; 01 represents 1; 10 represents 01+01, carrying one to the next place, so it is 2; and 11 is 10+01, so it is 3.
That is right: this is how binary represents data.
In computers, we generally use binary for basic operations. The reason is actually quite obvious: if we used decimal, we would need to design ten different circuits for ten different states. Binary is much simpler—with a basic electrical wire, powered means 1 and unpowered means 0.
Converting Binary
Suppose you are given a random binary number, 010110. How do we know what number it corresponds to?
Let us first look at a decimal number.
Take any decimal number, such as 21367. It can be broken down intuitively as
Then, intuitively, for the base- number 21367, we can write
More generally, a base- number can be expressed as
Feeling dizzy? Let us use the binary number 010110 directly as an example:
So its corresponding decimal value is .
See? Is that not simple?
Hexadecimal Numbers
You must have heard the terms 32-bit operating system and 64-bit operating system.
That is right: a 32-bit operating system means that an int-type variable has a total of 32 bits for storing a number.
In other words, an integer binary number can be up to 32 bits long.
That is a lot! Surely programmers debugging a program cannot point at the zeros and ones on the screen and count them one by one with their fingers?
And so we introduce hexadecimal numbers.
We define as , as , as , and so on, up to as .
Thus, the complete set of single hexadecimal digits is .
Your astonishing powers of observation must have noticed that this corresponds exactly to a 4-bit binary number!
Therefore, we can group the long binary number 01101011 into two hexadecimal digits, 6B.
2. Encoded Representations of Binary Numbers
Machine Numbers
By now, you must be able to perform addition and subtraction with binary numbers proficiently. So let us consider the following situation.
Suppose a computer register has only eight positions—that is, it can store only eight zeros or ones. How should we store positive and negative numbers?
Easy. Take the first position and define it as follows:
If the first position stores 0, the number is positive; if it stores 1, the number is negative.
The smallest number we can represent is therefore 11111111, or .
The largest is 01111111, or 127.
But now we have a headache: how do we perform operations on them?
Sign-Magnitude vs. Two’s Complement
We know that, to simplify the components inside the CPU as much as possible and maximize resource utilization, the basic components of our CPU can perform only addition.
For two positive numbers, calculation is easy. For example, :
Let us try making the numbers a little larger:
There is an extra bit! Unfortunately, our computer can store only 8 bits, so although we put the extra 1 in the sign bit, that is incorrect. After removing the sign bit, the result is
This is an overflow during computation, causing the computed result to differ from the actual result.
But your astonishing powers of observation have spotted something else: the result is 2… and that happens to be…?!
That is right. Calculating this way is exactly equivalent to taking the result modulo . Could we cleverly exploit this property to implement subtraction through addition?
For example, suppose we have two numbers, and , and need to calculate .
Since, on this computer, all of our results are
we can regard as
Wow! Genius! The audience erupts in delight!
How do we represent this with binary numbers?
Here, we introduce the concept of two’s complement.
The sign-magnitude binary representation of is 1001 1110.
Now invert every bit except the sign bit to obtain the one’s complement: 1110 0001.
Then add one to the last bit to obtain the two’s complement representation: 1110 0010.
For positive numbers, we define their two’s complement representation as the number itself.
Thus, for , we can use during the operation, turning the calculation into
Discarding the overflowing highest bit gives us 0001 0100, which is .
Other Benefits of Two’s Complement
If we use sign-magnitude representation, we encounter an awkward situation: zero has two representations.
That is, 1000 0000 represents -0, while 0000 0000 represents +0.
With two’s complement, however, we can define 1000 0000 as . This lets us store one more number than sign-magnitude representation can! The range also expands to –.
Signed vs. Unsigned Numbers
The signed numbers above are called signed numbers. They are stored in computers in two’s complement form.
For some other needs, however, we do not require signed numbers. In that case, we store them directly in the computer in sign-magnitude form, with no sign bit.
For example, on an 8-bit operating system, if you write
unsigned int i = 129;
then it directly stores 1000 0001. Because no sign bit needs to be stored, the storage range of an unsigned number on an 8-bit machine is 0–255.
Two: Addition and Subtraction Circuits
In a computer, a traditional arithmetic unit consists of an arithmetic logic unit (ALU), a shifter, a program status word (PSW) register, a general-purpose register set, and other components.
But let us not worry about all that yet. First, let us look at the ALU. The core component of an ALU is the adder.
1. One-Bit Full Adder
Let us recall how we calculate by hand.
When calculating , we use column addition:
That is, we add each pair of digits separately; if the sum is at least 2, we carry 1 into the next higher place.
A computer performs addition using the same idea. Let us first imagine the simplest addition circuit: a one-bit full adder.
When performing addition for a single position, we need to process three inputs: addend , addend , and the carry from the lower position. It also produces two outputs: the current-position sum and the carry to the higher position.
Let us first analyze how to calculate :
- When and are the same, the current-position digit of their sum must be
0. After all, and . - When and differ, the current-position digit of their sum must be
1.
Following this idea, the binary calculation can be represented by the XOR logic circuit, namely .
Now let us put the carry into this addition expression in the same way: if and are the same, the result is 0; if and differ, the result is 1. In other words, the calculation can be represented as . Therefore:
Next, let us look at the carry :
- When both and are
1, a carry is produced. After all, . - When only one of and is
1:- If the carry from the previous position’s operation is
1, a carry is produced. - If is
0, then , so there is certainly no carry.
- If the carry from the previous position’s operation is
In terms of the result: is 1 and is 1; or and differ and is 1.
Written as a logical expression, this is
The logical structure of a one-bit full adder is therefore shown below.
If we encapsulate it and expose only its inputs and outputs, its logic symbol is:
2. Ripple-Carry Adder
Cascading full adders forms an -bit serial-carry adder (also called a ripple-carry adder), as shown below.
Chaining them together this way makes it easy to add two binary numbers.
Encapsulating them as a single unit gives us a traditional adder.
3. Adder with Flags
For an -bit adder, besides obtaining the result, we often need to know whether overflow occurred during the calculation, whether the result is positive or negative, whether the result is zero, and so on.
We require the adder to generate the following flags:
- : overflow flag.
1indicates overflow, while0indicates no overflow. - : sign flag. It equals the most significant bit of the result;
1indicates negative, while0indicates positive. - : zero flag.
1indicates that the result of the addition or subtraction is . It is set to1when every bit is0. - : carry/borrow flag. It is used to determine whether overflow occurred in an unsigned addition or subtraction.
1indicates overflow, while0indicates no overflow.
The adder’s symbol can be represented as follows:
Methods for Detecting Overflow
We know that addition and subtraction using two’s complement can overflow only when adding numbers with the same sign or subtracting numbers with different signs. This is easy to understand intuitively, because when two numbers with the same sign are subtracted, the absolute value of the result must be smaller than the absolute value of either the subtrahend or the minuend.
On an 8-bit machine, when a result exceeds 127, it wraps around to -128. For example, . Based on this, we can devise a very intuitive detection method: when two numbers have the same sign, overflow has occurred if the sign of the result differs from theirs.
1) Using One Sign Bit
Suppose the sign bits of the two numbers taking part in the operation are and , respectively, and the sign bit of the result is . We obtain the overflow logic expression:
2) Using One Sign Bit Together with the Carry
- If both numbers are positive, their sign bits are both
0, so they certainly do not produce a carry into the next position.- If the second-highest position does not carry into it, the sign has not changed, and the sign bit remains
0. - If the second-highest position does carry into it, the sign bit becomes
1; the sign has changed, so overflow has occurred.
- If the second-highest position does not carry into it, the sign has not changed, and the sign bit remains
- The same principle applies when both numbers are negative. Their sign bits are both
1, so they necessarily produce a carry into the next position, while the current-position digit after addition is0.- If the second-highest position carries
1into it, the sign bit remains1, and no overflow has occurred. - If the second-highest position does not carry into it, the sign bit is
0; the sign has changed, so overflow has occurred.
- If the second-highest position carries
To summarize, overflow occurs if the carry from the highest position differs from the carry from the second-highest position.
In other words, suppose the carry produced by the sign bit after the calculation is , and the carry produced by the second-highest position (the highest value bit) is . If differs from , overflow is indicated.
Three: Addition and Subtraction Circuit
Finally! Here comes the big one! But before that, we need to cover a small interlude.
1. MUX Multiplexer
In an addition and subtraction circuit, we need a 2-to-1 multiplexer (MUX) to control whether addition or subtraction is performed.
The idea is simple: for subtraction, take the additive inverse of the subtrahend. That is,
We feed both and into the MUX and give it a signal, Sub.
- If Sub is
0, addition is being performed, so the MUX selects as its output. - If Sub is
1, subtraction is being performed, so the MUX selects as its output.
is the inversion of . If , then .
2. Arithmetic Circuit
Connecting the MUX to one end of the adder gives us the following circuit:
In a computer, addition and subtraction of both signed and unsigned numbers are implemented using this same circuit. Its inputs include two -bit operands, and , as well as a control signal, .
The control signal not only determines which data path enters the adder; when subtraction is performed (when is 1), it also serves as the carry input to the lowest bit.
Now everything is ready. Let us analyze, step by step, how addition and subtraction work.
Addition
- is fed directly into the adder.
- is
0, so the MUX selects and feeds it into the adder. - The adder directly computes , outputs the -bit result and the carry output , and generates the status flags.
It is worth noting that if and are unsigned numbers, then the result is . If , a carry is produced, so , indicating that unsigned overflow has occurred.
Subtraction
- is fed directly into the adder.
- is
1, so the MUX selects and feeds it into the adder. - The adder computes , namely .
If we are calculating the subtraction of signed numbers, then inverting and adding 1 gives exactly , so the operation is equivalent to .
In addition, when calculating unsigned subtraction, we define the overflow flag OF as the inverse of . That is, . This is exactly the opposite of unsigned addition.
This is because the operation is equivalent to . Therefore:
- When , , so there is a carry.
- When , , so there is no carry and . At this point, , indicating overflow.
Epilogue
Your mom bursts through the door and glares furiously at your computer screen—but by now, you have Cloverta’s Blog open!
It turns out that you were learning binary addition and subtraction. She nods in satisfaction. This person’s blog is pretty good, so remember to add its RSS feed to your subscriptions.
comments
Feel free to leave your thoughts here. 💭💡
After signing in, you can click the “Subscribe by Email” button at the bottom right of the text box to receive notifications of new interactions via email.