C language with bit operation to achieve the addition of skills

  • 2020-04-01 21:31:01
  • OfStack

With bit operation to achieve addition is the computer with binary operation, 32 bit CPU can only represent the number in 32 bits, here to use the addition of 1 bit, on the basis of not considering the carry, as follows

 
1 + 1 = 0 
1 + 0 = 1 
0 + 1 = 1 
0 + 0 = 0 

It is obvious that these expressions can be replaced by the bit operation "^", as follows
 
1 ^ 1 = 0 
1 ^ 0 = 1 
0 ^ 1 = 1 
0 ^ 0 = 0 

So we've done simple one-digit addition, so is it feasible to do two-digit addition? Of course not. The paradox is, how
Get carry? To get the carry we can think of it as follows:
 
0 + 0 = 0 
1 + 0 = 0 
0 + 1 = 0 
1 + 1 = 1 

// on the other hand, it looks like this
 
0 & 0 =  Don't carry  
1 & 0 =  Don't carry  
0 & 1 =  Don't carry  
1 & 1 =  carry  

As it happens, in bit operations, we use "< <" Means to move one bit to the left, or carry. So we get the following expression
 
//Carry can be expressed as follows:
(x&y)<<1 

So by this point, we basically have these two expressions
 
x^y //Perform addition
(x&y)<<1 //Carry the operation

Let's do a two-digit number addition, without taking into account the carry
 
11+01 = 100 //Original algorithm
//It is calculated using a calculated expression
11 ^ 01 = 10 
(11 & 01) << 1 = 10 
//So here we have 10 plus 10 is equal to 100 when we do the normal addition between these two Numbers
//But we don't need to add, so the other way to think about it is, what if I take these two Numbers and compute them the same way I did before
10 ^ 10 = 00 
(10 & 10) << 1 = 100 

So that's pretty much the answer, but you don't have to evaluate the "00" anymore, because the first expression already gives you the answer.
So if you keep going, you can add three Numbers and you just have to do it three times to get the value of the first expression.
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; 
} 

Computers are essentially binary operations, and many pundits and gurus have shown how to use bit-arithmetic to do confusing and surprising things. I saw a log on douban describing how to use bit operation to realize multiplication. In fact, the key to solve the problem is how to use bit operation to realize addition. I think the original statement is not accurate enough.
Theorem 1: if a and b are two binary Numbers, a+b = a^b + (a&b)< < 1.
Proof: a^b is the result of addition without considering carry. Carry occurs when both bits are 1, so (a&b)< < 1 is the value produced by the carry and is called carry compensation. Add them together and you get a complete addition.
Theorem 2: theorem 1 can be used to achieve only bit operations for addition operations.
Proof: use the equation in theorem 1 to continuously iterate on itself. With each iteration, there is an extra zero on the right side of the carry compensation, so at most, the length of the binary bit needs to be added.


Related articles: