The use of bitwise operations to achieve addition is the computer with the binary operation, 32-bit CPU can only represent the number of 32 bits, where the first 1-digit addition to proceed, without regard to carry, based on the following 1 + 1 = 0 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0
It is obvious that these expressions can be replaced by the "^" of the bitwise operation, as follows 1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0
So that we can complete a simple one-number addition, then the two-bit addition, this method is not feasible. Must be no, contradiction lies in, how to
Gets the rounding. To get the carry, we can think about it as follows: 0 + 0 = 0 1 + 0 = 0 0 + 1 = 0 1 + 1 = 1//from a different angle. 0 & 0 = Do not carry 1 & 0 = Do not carry 0 & 1 = Do not carry 1 & 1 = rounding
Well, in a bitwise operation, we use "<<" to move one bit to the left, which means "carry." Then we can get the following expression//carry can be expressed as follows: (x&y) <<1
Here we basically have such two expressions X^y//Perform addition (X&Y) <<1//Carry Operations
Let's do a 2-digit addition, without considering the carry, 11+01 = 100//The original algorithm//computed by the calculated expression ^ (&) << 1 = 10//Here we use ordinary addition to You can get 10 + 10 = 100 at the time of operation of these two numbers, but we do not need to add, so to think of another way, if you let the two number again according to the algorithm of the first one? ^ ten = XX (&) << 1 = 100
Basically come to the conclusion, in fact, the back of the "00" is no longer to calculate, because the first expression has been calculated results.
The continuation of inference can be concluded that the three-digit addition requires a repeated calculation of three times to get the value of the first expression is the computed result.
The C code is as follows:
int Add (int a,int b)
{
int jw=a&b;
int jg=a^b;
while (JW)
{
int T_A=JG;
int t_b=jw<<1;
jw=t_a&t_b;
Jg=t_a^t_b;
}
return JG;
}
The computer is essentially a binary operation, and many tall people and the Heavenly Book show how to use bitwise arithmetic to realize the tangled but surprising thing. In the watercress see a log describes how to use bit operations to achieve multiplication, in fact, the key to solve the problem is how to use bit operations to achieve the addition. It is summarized as follows that the original narrative is not accurate enough.
Theorem 1: Set A, b to two binary numbers, then a+b = A^b + (a&b) <<1.
Proof: A^b is the addition result when rounding is not considered. When the bits is 1 o'clock at the same time, the carry is only, so (a&b) <<1 is the value produced by the carry, called the carry compensation. Adding the two is the result of the complete addition.
Theorem 2: The use of theorem 1 can be achieved only by the bitwise operation of the addition operation.
Proof: Use the equation in theorem 1 to iterate over itself. For each iteration, the carry compensation is more than one 0 on the right, so the maximum number of addend bits length iterations is required, and the carry compensation becomes 0, when the operation ends.