65 do not have to prove safety offer- face questions do addition and subtraction, multiplication and division - bit computing

/*
topic:
    Without the use of add, subtract, and calculates two integers.
Ideas:
    You can not use addition, subtraction can only consider bit computing.
    x = num1 ^ num2, compared to erase the results into bits.
	y = num1 & num2, is only the result of the carry.
	(Y << 1) & x, until a carry is not generated.
*/
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

int Add(int num1, int num2)
{
    int x = num1 ^ num2;
    int y = num1 & num2;
    while(y != 0){
        int temp = x ^ (y << 1);
        y = x & (y << 1);
        x = temp;
    }
    return x;
}
int main () {
   cout<<Add(5,17);
}

   

Guess you like

Origin www.cnblogs.com/buaaZhhx/p/12141662.html