A+B(加法器实现)

北慕城南 提交于 2019-12-05 15:23:10
#include<iostream>
#include<vector>
using namespace std;
int main()
{
        int k, a, b, t1, t2, ans;
        while(cin >> a >> b) {
        k = 0;
        ans = 0;
        for (unsigned int i = 1; i; i *= 2)
        {
            t1 = (a & i) ? 1 : 0;
            t2 = (b & i) ? 1 : 0;
            if (t1 == 1 && t2 == 1)
            {
                if (k == 1) ans = ans | i;
                k = 1;
            }
            else if (t1 == 1 || t2 == 1)
            {
                if (k == 0) ans = ans | i;
            }
            else
            {
                if (k == 1) ans = ans | i;
                k = 0;
            }
        }
        printf("%d\n", ans);
        }
}

a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!