编写一个C程序来模仿一个2位半加法器 [英] Write a C program to imitate a 2-bit half adder

查看:105
本文介绍了编写一个C程序来模仿一个2位半加法器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C编程中模仿2位半加法器的代码



我尝试过:



#include< stdio.h>



typedef char bit;

bit carry = 0;

位halfadd(位A,位B){

carry = A& B;

返回A ^ B;

}

code to imitate a 2-bit half adder in C programming

What I have tried:

#include<stdio.h>

typedef char bit;
bit carry = 0;
bit halfadd( bit A, bit B ){
carry = A & B;
return A ^ B;
}

推荐答案

现在您需要做的就是添加代码调用你的函数!

Now all you need to do is add the code to call your function!
int main()
{
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            int result = halfadd(i, j);
            printf("%d + %d = %d:%d\n", i, j, result, carry);
        }
    }
    return 0;
}

虽然说实话,您可能希望在计算中包含任何现有的进位...

Though to be honest, you might want to include any existing carry in your calculation...


这篇关于编写一个C程序来模仿一个2位半加法器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆