#继电器逻辑# 2位全加/全减器真值表
2012-09-07 16:54阅读:

以下是生成这个真值表的C#代码。
using System;
namespace TwoBitAdderTruthTable
{
class Program
{
static void
Main(string[] args)
{
int a, b, ci, s;
Console.WriteLine('CI\tA1\tA0\tB1\tB0\tCO\tS1\tS0');
for (ci = 0; ci < 2; ci++)
{
for (a = 0;
a < 4; a++)
{
for (b = 0; b < 4; b++)
{
// s = a +
b + ci; // For add
s = a - b -
ci; // For sub
int a1 = (a
& 0x00000002) >> 1;
int a0 = a
& 0x00000001;
int b1 = (b
& 0x00000002) >> 1;
int b0 = b
& 0x00000001;
int co = (s
& 0x00000004) >> 2;
int s1 = (s
& 0x00000002) >> 1;
int s0 = s
& 0x00000001;
Console.WriteLine('{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}',
ci, a1, a0, b1, b0, co, s1, s0);
}
}
}
}
}
}