How to use a plus sign to calculate the sum of three numbers

Source: Internet
Author: User
Tags bitwise operators

The method for calculating the three numbers is( a ^ b ^ c ) + ( ( ( a & b ) | ( b & c ) | ( a & c ) ) << 1 )...

But if you want to ask where this formula comes from...Addition...

Before learning how to start, how can we get started...

First, you need to know that the numbers in the computer are stored in binary... the numbers you see... in the computer's view are both 0 and 1...

We can randomly pick two numbers, such as 123 and 321, and convert them to binary, and then add them together in a vertical way...

    '    "'    001111011  = 123 + 101000001  = 321-------------   110111100  = 444

Alignment in the same position... from the single digit plus... the result of the addition is filled with two in one... I don't need to say that ..?

You may notice that I have added'And"Symbol
... This indicates carry...

The difference between the two symbols is:'Represents the carry produced by the original number..."Indicates'Generate carry
...

But in fact, the "carry" event is also binary... 0 and 1... so we can use binary to represent it...

Place all carry values generated from the original number, that is'Write 1... other input 0... the result is like this...

   001111011  ( a   101000001  ( b   010000010  ( c

We get three numbers... A and B are the original numbers... C is the carry...

Think back to the process of vertical calculation... is it true that a bit of value is first calculated every time... and then we can see if there is a carry to be added ..?

We now use a program to simulate this process... when we do not consider carrying, we first add a and B...

When there is only one digit...0 ^ 0 = 0 1
^ 0 = 1
 0 ^ 1 = 1 1
^ 1 = 0
... Only in these four cases...

I wonder if you have found out... in this case... I'm not using+Instead^The same result is returned.
...

^It is in bitwise operators.Bitwise OR...
The simple explanation is that if two numbers are the same, 0 is returned, and 1 is returned...

Here we will not go into detail about bit operations...

In short, after we add... A and B without carrying... our formula becomes like this...

         '   010000010  ( c + 100111010  ( d-------------   110111100  = 444

Whered = a ^ b... In this way, we have successfully converted the question of adding a and B into the question of adding C and D...

But at the same time, there are new problems... how can we get C ..?

Let's look back at how. c came from ..? It is converted from carry... How does carry come from ..? Full two into one...

That is, when one of the positions of A and B is 1 at the same time, the left of the corresponding position of... C is 1...

In other words... when the nth plus 1 bits from the left of A and B are both 1, the nth bits from the left of C are 1...

Which is described as follows:c = ( a & b ) << 1...

&Is In bitwise operatorsBitwise AND...<<Is In bitwise operatorsMove left...

If you carefully read the answer to another question I just mentioned... you should not have any questions about these two operators...

So far... We have converted C and D back to the formulas related to a and B...

To sum up... the result of the carry calculation isa ^ b... The carry part is(
a & b ) << 1
...

So we can draw a conclusion...a + b = ( a ^ b ) + ( ( a & b ) << 1 )...

We learned how to change the sum of two numbers to the sum of the other two numbers...

This seemingly useless formula... is precisely the key to solving this problem...

If you want to... you can keep writing it like this. For example, you can split this sub-statement into the following...

(a ^ b) ^ ( ( a & b ) << 1 ) + ( ( a ^ b ) & ( ( a & b ) << 1 ) )

Then continue... until the item on the rightmost is shifted to 0 after several times... you have implemented addition without the plus sign...

At this time, you add a new number... to add the three digits of a plus sign...

Of course, if you still want to continue, you can even add three numbers without the plus sign...

The only problem is that the final formula cannot be displayed on a screen...

So now we are going back to the beginning... continue to the vertical column... to see what happened when the three numbers are added...

Let's just pick three numbers... for example, 111 222 and 333...

   ""#""##''   0001101111  = 111   0011011110  = 222 + 0101001101  = 333--------------   1010011010  = 666

Same as before'Represents the carry produced by the original number..."Indicates'Generate carry
...

Added#It indicates that the original number will generate carry here, but because'Or"Causes the carry position of the Left Shift
...

We also represent this carry as binary...'And#As
1 others are recorded as 0...

   0001101111  ( a   0011011110  ( b   0101001101  ( c   0010011110  ( d

I think it's all over now... you should be able to see the rule as well...

As long as one of the three numbers of a B and C has at least two of them as 1... the left of the corresponding position of D is 1...

Which is described as follows:d = ( ( a & b ) | ( b & c ) | ( a & c ) ) << 1...

|Is In bitwise operatorsBy bit or...
Compare the three numbers to find whether the same bitwise of one or two numbers is 1...

After the carry is calculated, I don't need to say anything about the carry calculation result ..?

Now let's go back and look at the formula we mentioned at the beginning... do you think it's suddenly clear ..?

I finally finished writing... this answer was written for almost two hours... the final headers are all big... I don't know what I'm talking about...

Not all of the answers are correct when there are errors...

Teaching the children is really physical... comfort...

And... leave a question for you at last... if you can do it, you will not waste me so much effort to write so much...

Realize the multiplication of two numbers without having to multiply the number... if you can do it, you really understand...

That's it...

Reprinted from: http://nodejs.iteye.com/blog/1855220

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.