不用加减乘除运算符做加法

题目描述

写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

个人笔记

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    int t;
    if(b==0)
    {
        cout<<a<<endl;
        return 0;
    }
    if(a==0)
    {
        cout<<b<<endl;
    }
    while(b!=0)
    {
        t = a^b;
        int s = (a&b)<<1;
        a = t;
        b = s;
    }
    cout<<t<<endl;


    return 0;
}

猜你喜欢

转载自blog.csdn.net/wys5wys/article/details/80714545