0

さらに情報が必要な場合は、お問い合わせください。

私がやろうとしているのは、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));
}

bool nor(bool a, bool b)
{
return nand(nand(nand(a,a), nand(b,b)),nand(nand(a,a), nand(b,b)));
}

add関数のコード:

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 *a = int32tobinary(32);
bool *b = int32tobinary(32);
bool *c = add32(a, b, false);
__int32 i = binarytoint32(c);

これらの2つの機能は次のとおりです。

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;
}
4

1 に答える 1

1

どこから始めれば?

コメントにあるように、静的変数へのポインタを返すのは間違っています。

これ

out[0] = nor(nor(a, b), carry); 

する必要があります

out[0] = xor(xor(a, b), carry);

これout[1] = or(and(b,carry),and(a,b));も誤りです。out[1]である必要がtrueありa == trueますcarry == true

add32インデックス 0 が LSB であるint32tobinaryint32tobinary仮定し、インデックス 0 が MSB であると仮定します。

于 2012-09-11T10:57:29.603 に答える