A+b
Required to give two integers a and B, and to ask for them, but not to use + such mathematical operators. Here I have extended, add the multiplication function, the addition function is through detection, as to multiplication function, I measured a few groups of data is no problem, as for the existence of no loopholes, I am not good to say, for reference only.
PackageCom.zhao.project; Public classproject_1 { Public Static voidMain (string[] args) {project_1 Project=Newproject_1 (); intsum = PROJECT.APLUSB (6,-1); System.out.println ("Sum:" +sum); Sum=PROJECT.CHENGFA (-1, 3); System.out.println ("Result:" +sum); } /** param a:the First integer param b:the Second integer return:the sum of * A and B*/ Public intAPLUSB (intAintb) {/** Both bitwise AND & are 1, the result is 1 otherwise all 0. Bitwise OR | Both are 0, and the result is 0 or 1. Bitwise XOR ^ Same as 0 instead of 1*/ intc = A &b; if(c! = 0) {a= a ^C; b= b ^C; C= C << 1; intresult = Aplusb (A |b, c); returnresult; } Else { returnA |b; } } Public intCHENGFA (intAintb) { intC=0; intSum=0; for(inti=0;i<31;i++) {C=b&1; if(c!=0) {sum=aplusb (a<<i, sum); } b=b>>1; } returnsum; }}
Additive Analysis:
There are only 5 operations for bit operations: with, OR, XOR, shift left, right, bitwise AND & both are 1, the result is 1 otherwise 0. Bitwise OR | Both are 0, and the result is 0 or 1. Bitwise XOR ^ is the same as 0 instead of 1. The left shift operator M<<n indicates that m shifts n bits to the left. When the n bit is shifted to the left, the leftmost n bits are discarded, with n 0 on the far right. The right-shift operator m>>n means to shift m to the right N-bit. When you move the n-bit right, the rightmost n bit is discarded.
I used the 6+1,6=0110,1=0001 when I did the addition, and obviously the two were bitwise OR, 0111, and the result was correct. However, if it is 6+3, that is, 0110+0011, the existence of a carry problem will fail. The most important thing to do for this addition is to solve this rounding problem. 0 1 1 0 0 0 1 1c=a&b=0010 We need to set the third bit of the result to 0 and then add 1 to the second. Obviously we operate on the basis of a|b=0111. A^c= get the remainder of a in addition to the one that needs to be rounded up, B^c gets the rest of the number in B. At this point A and B are obviously not at the same time a bit of 1. This is very convenient a|b to get the results, the current result is 0101, the third bit is set to 0. Also need to complete carry is 0101+0100, we have 0010, is through the first a&b get, can show the third place to see, want to know in which a +1,c=c<<1; get C is 0100, then our problem is 0101+ 0100, obviously the second place is 21, the problem repeats, OK, handed back. We then deal with these two numbers, as a and B, only A and B do not have the same bit as 1, we use a|b to get the results.
Multiplication Analysis:
It's 0110 and 0011.
0 1 1 0
* 0 0 1 1
0 1 1 0
0 1 1 0
1 0 0) 1 0
After doing this on paper, I think it seems like addition. The idea is clearer, that is, several numbers are added together. a=0110 b=0011, we just need to get every bit of B, not 0 is 1, there is a 1 to add a 0110, 0 is not added. However, it is important to note that 0110 will be 0 to complement how much 0 depends on the situation.
Since it is cumulative, there must be sum=0,b in addition to the sign bit, there should be 31 bits, good, loop 31 times, in turn get each of the B. c=b&1; this to clear the front of B, only the last one. If 1 A is shifted to the left, then the sum is added. Why move left, you can look at the multiplication of the formula, there is a go to the left of the process, so when i=0, that is, the first time, is not left to move, it can be said to move left to 0. Each time we move the corresponding number of digits to the left, fortunately we have the I variable, can easily achieve this process. Whether C is 0 or 1 B requires a right shift, and each move one bit, so as to ensure that we read B the last one can be b this 32-bit positive read all, of course, read 31 bit is enough. The process of addition is the same as above.
A+b problem Extension