C++ 二进制逻辑仿真器逻辑错误
如果你需要更多的信息,尽管问 <>我想做的是模仿布尔逻辑,它可以在C++代码的计算机上找到。现在我正在尝试创建一个32位加法器。当我运行测试代码时,我得到的输出是32,这是错误的,应该是64。我相当确信我的C++ 二进制逻辑仿真器逻辑错误,c++,boolean,logic,C++,Boolean,Logic,如果你需要更多的信息,尽管问 我想做的是模仿布尔逻辑,它可以在C++代码的计算机上找到。现在我正在尝试创建一个32位加法器。当我运行测试代码时,我得到的输出是32,这是错误的,应该是64。我相当确信我的add函数是正确的。这些闸门的代码是: bool and(bool a, bool b) { return nand(nand(a,b),nand(a,b)); } bool or(bool a, bool b) { return nand(nand(a,a),nand(b,b)); } bo
add
函数是正确的。这些闸门的代码是:
bool and(bool a, bool b)
{
return nand(nand(a,b),nand(a,b));
}
bool or(bool a, bool b)
{
return nand(nand(a,a),nand(b,b));
}
bool nor(bool a, bool b)
{
return nand(nand(nand(a,a), nand(b,b)),nand(nand(a,a), nand(b,b)));
}
添加函数的代码:
bool *add(bool a, bool b, bool carry)
{
static bool out[2];
out[0] = nor(nor(a, b), carry);
out[1] = or(and(b,carry),and(a,b));
return out;
}
bool *add32(bool a[32], bool b[32], bool carry)
{
static bool out[33];
bool *tout;
for(int i = 0; i < 32; i++)
{
tout = add(a[i], b[i], (i==0)?false:tout[1]);
out[i] = tout[0];
}
out[32] = tout[1];
return out;
}
这两项职能是:
bool *int32tobinary(__int32 a)
{
static bool _out[32];
bool *out = _out;
int i;
for(i = 31; i >= 0; i--)
{
out[i] = (a&1) ? true : false;
a >>= 1;
}
return out;
}
__int32 binarytoint32(bool b[32])
{
int result = 0;
int i;
for(i = 0; i < 32; i++)
{
if(b[i] == true)
result += (int)pow(2.0f, 32 - i - 1);
}
return result;
}
bool*int32tobinary(uu int32a)
{
静态布尔输出[32];
bool*out=\u out;
int i;
对于(i=31;i>=0;i--)
{
out[i]=(a&1)?真:假;
a>>=1;
}
返回;
}
__int32二进制到int32(布尔b[32])
{
int结果=0;
int i;
对于(i=0;i<32;i++)
{
如果(b[i]==true)
结果+=(int)功率(2.0f,32-i-1);
}
返回结果;
}
从哪里开始
如注释中所述,返回指向静态变量的指针是错误的
这个
应该是
out[0] = xor(xor(a, b), carry);
这out[1]=或(和(b,进位)和(a,b))代码>也不正确<当a==true
和carry==true
时,code>out[1]
必须为true
add32
假设索引0为LSB,int32tobinary
和int32tobinary
假设索引0为MSB。您尝试了什么?类似于逐步/printf调试的东西,看看它在什么程度上偏离了您的预期?听起来像是调试器的工作。在任何情况下,返回一个指向静态变量的指针通常都是个坏主意。<代码>或和I
应该从LS位开始,这似乎是代码中的第31位?我想,进位也在tout的错误端?在add中,我将out[1]=行更改为:out[1]=或(和(xor(a,b),进位),和(a,b));
根据这里的图表,我认为这是正确的:“全加器逻辑图”
out[0] = nor(nor(a, b), carry);
out[0] = xor(xor(a, b), carry);