Think in terms of two or three bits, and then understand that these things scale to 32 or 64 or any other bits.
Start with decimal
99 +22
To do this, we will have a few Migrate.
11 99 +22
9 plus 2 - 1, one, 1 plus 9 plus 2 - 2, one ...
The thing is, having noticed that to add two numbers I really need three lines, at least for some of them I may need to add three numbers. The same thing with an adder in alu, each column or bit strip, a one-bit adder, should be able to add two inputs plus a carry bit, and the result should be one bit and one bit carry.
Since you used 5 and 2, you can do 4 bit binary math
0101 +0010
We did not need to transfer this, but you can see that the mathematics worked, 5 + 2 = 7.
And if we want to add 5 and -2
11 0101 +1110
And the answer will be 3, as expected, not surprisingly, but we had the execution. And since it was an addition with a minus digit in two additions, it all worked, there was no sign bit, and then two additions were added, so we donβt just need to feed the bag with two operands.
Now, if you want to make a small difference, that if you want to subtract 2 from 5, you select the subtraction command, not add it. Well, we all learned that negation in a double complement means inverting and adding one. And we saw above that the two input adders really need a third input for the transfer, so that it can be cascaded, no matter how wide the adder is. Therefore, instead of doing two addition operations, invert and add 1, being the first addition of the real addition, all we need to do is invert and set the transfer:
Understand that there is no subtraction logic; it adds a negative result to the fact that you feed it.
v this bit is normally zero, for a subtract we set this carry in bit 11 11 0101 five +1101 ones complement of 2 ===== 0011
And what do you know, we get the same answer ... It doesn't matter what the actual values ββare for any of the operands. if this is an add operation, you put zero on the carry bit and feed it into the adder. If this is a subtraction operation, you invert the second operand and put one on the transfer and feed it to the same adder. Everything that falls out falls out. If your logic has enough bits to store the result, then it all works, if you don't have enough space, then you overflow.
There are two types of overflow, unsigned and signed. Unsigned is just a carry bit. The signed overflow is related to comparing the carry bits in the msbit column with the execution bit for that column. For our maths above, you see that the carry and carry of this msbit column is the same, both are the same. And we know that in a 4-bit system there is enough space for the correct representation of the numbers +5, -2 and +3. A 4-bit system can display the numbers +7 to -8. Therefore, if you need to add 5 and 5 or -6 and -3, you will get a signed overflow.
01 1 0101 +0101
Understand that the SAME add logic is used for signed and unsigned maths, it depends on your code, not the logic, to actually determine if these bits were considered two additions or without sign.
In the 5 + 5 example above, you see that the carry in the msbit column is 1, but the execution is 0, which means that the V flag, the signed overflow flag, will be set by logic. At the same time, the execution of this bit, which is the C flag, the carry flag will not be set. When 4-bit unsigned thinking can contain numbers from 0 to 15, so 5 + 5 = 10 is not overflowing. But when thinking, signed with 4 bits, can contain from +7 to -8, and 5 + 5 = 10 is an overflow with a signature, so flag V is set.
if / when you have the add with carry command, they accept the SAME scheme with summation, and instead of submitting the carry to zero, it gets the carry flag. Similarly, a subtraction with a borrow, instead of applying a carry of 1, has a value of 1 or 0 based on the status of the carry flag in the status register.
Multiplication is a whole different story, binary code makes multiplication much easier than when it is performed with decimal mathematics, but you must have different instructions without specifying a sign and a subscription. And separation is a separate, separate beast, so most instruction sets do not have a gap. Many of them have no breeding due to the number of windows or hours that it burns.