Introduction to addition using bitwise operations in C Language

Source: Internet
Author: User

The bitwise operation is used to implement addition, that is, the computer performs binary operations. A 32-bit CPU can only represent the number within 32 bits. Here we use the one-digit addition, the following code does not consider carry.

Copy codeThe Code is as follows: 1 + 1 = 0
1 + 0 = 1
0 + 1 = 1
0 + 0 = 0

Obviously, these expressions can be replaced by the bitwise operation "^", as shown below:Copy codeThe Code is as follows: 1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

In this way, we have completed a simple one-digit addition, so we need to perform two-digit addition. Is this method feasible? Certainly not. The contradiction lies in how to proceed.
Get carry? To obtain the carry value, we can consider the following:Copy codeThe Code is as follows: 0 + 0 = 0
1 + 0 = 0
0 + 1 = 0
1 + 1 = 1

// From another perspectiveCopy codeThe Code is as follows: 0 & 0 = do not carry
1 & 0 = do not carry
0 & 1 = do not carry
1 & 1 = carry

In the bitwise operation, we use "<" to move a bit to the left, that is, "Carry ". Then we can get the following expression:Copy codeThe Code is as follows: // carry can be expressed as follows:
(X & y) <1

Here, we basically have these two expressionsCopy codeThe Code is as follows: x ^ y // execute Addition
(X & y) <1 // carry operation

Let's do a two-digit addition without considering carry.Copy codeCode: 11 + 01 = 100 // original algorithm
// Calculated using a calculated expression
11 ^ 01 = 10
(11 & 01) <1 = 10
// Here, when we use the ordinary addition to calculate the two numbers, we can get 10 + 10 = 100.
// But we do not need addition. So if we want to use another method to calculate the two numbers again
10 ^ 10 = 00
(10 & 10) <1 = 100

Here we come to the conclusion that, in fact, the "00" behind is no longer necessary to calculate, because the first expression has already produced the result.
Continuous inference can be used to obtain the addition of three numbers. Only three repeated computations are required to obtain the value of the first expression, which is the calculated result.
The c code is as follows:Copy codeThe 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 masters and books have demonstrated how to use bitwise operations to achieve tangled but surprising things. I saw a log on Douban to describe how to use bitwise operations to implement multiplication. The key to solving the problem is how to use bitwise operations to implement addition. I think the original description is not accurate enough. The summary is as follows.
Theorem 1: If a and B are two binary numbers, a + B = a ^ B + (a & B) <1.
Proof: a ^ B is the addition result without considering carry. When the binary bit is 1 at the same time, there is an advance. Therefore (a & B) <1 is the value generated by the carry, which is called the carry compensation. Adding the two is the complete addition result.
Theorem 2: Theorem 1 can be used to perform addition operations with only bitwise operations.
Proof: The equations in Theorem 1 are used to continuously iterate on itself. After each iteration, one more digit is 0 on the Right of carry compensation. Therefore, a maximum number of binary bit length iterations are required. The carry compensation is changed to 0, and the operation ends.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.