正直言って、私は本当にコードが良いとは思わない。それはそれに厄介なにおいを持っています。つまり、この関数は:
unsigned int i4_xor (unsigned int i, unsigned int j)
//****************************************************************************80
//
// Purpose:
//
// I4_XOR calculates the exclusive OR of two integers.
//
// Modified:
//
// 16 February 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, unsigned int I, J, two values whose exclusive OR is needed.
//
// Output, unsigned int I4_XOR, the exclusive OR of I and J.
//
{
unsigned int i2;
unsigned int j2;
unsigned int k;
unsigned int l;
k = 0;
l = 1;
while (i != 0 || j != 0)
{
i2 = i/2;
j2 = j/2;
if (
((i == 2 * i2) && (j != 2 * j2)) ||
((i != 2 * i2) && (j == 2 * j2)))
{
k = k + l;
}
i = i2;
j = j2;
l = 2 * l;
}
return k;
}
i8_xorもあります。そして2つのabs関数。
DailyWTFへの投稿が順調だと思います。
EDIT:
function xor i:unsigned, j:unsigned
answer = 0
bit_position = 1
while i <> 0 or j <> 0
if least significant bit of i <> least significant bit of j
answer = answer + bit_position
end if
bit_position = bit_position * 2
i = i/2
j = j/2
end while
return answer
end function
最下位ビットがセットまたはクリアされている場合、次のように使用されているかを決定するには:
を非Cプログラマのために、ここでは上記の何への迅速なガイドです
bit set if i <> (i/2) * 2
bit clear if i == (i/2) * 2
コードが余分なWTFyになるのは、CがXOR演算子、つまり '^'を定義することです。だから、代わりに:
result = i4_xor (a, b);
あなたが持つことができます。
result = a^b; // no function call at all!
元プログラマが本当にXOR演算子について知っている必要があります。しかし、たとえ彼らが(そしてそれが別の難読化されたCのシンボルである)許されないとしても、XOR関数の実装は信じられないほど貧弱です。
私は主にPython開発者ですので、このコードは一般的に読めないこと以外は何も実際には間違っています。 –
このコードはFortran 77から移植されたとのコメントがあります。Fortran 77にはXOR演算子がありますか? – bk1e