我有一些 Crc-64 表生成器的示例代码。我试图检查无符号整数的符号,发现它会生成负整数和正整数的混合表常量。Crc-64 校验和也一样,它可能是负数或正数。是否可以实现一个修改后的 Crc-64 表生成器,它应该生成所有负符号常量以及校验和?或者其他所有正符号常量和校验和。请帮助我提供一些信息和示例实现。
下面是 Crc-64 表生成器的示例代码:
public class Crc64
{
public const UInt64 POLYNOMIAL = 0xD800000000000000;
public UInt64[] CRCTable;
public Crc64()
{
this.CRCTable = new ulong[256];
for (int i = 0; i <= 255; i++)
{
int j;
UInt64 part = (UInt64)i;
for (j = 0; j < 8; j++)
{
if ((part & 1) != 0)
part = (part >> 1) ^ POLYNOMIAL;
else
part >>= 1;
}
CRCTable[i] = part;
}
}
}
更新:请告知,根据我的问题,此实现是否正确:
public static List<UInt64> generateCrcTable(UInt64 POLY)
{
const ulong TOPBIT_MASK = 0x8000000000000000;
const ulong LOWBIT_MASK = 0x0000000000000001;
List<UInt64> list = new List<UInt64>();
for (int i = 0; i <= 255; i++)
{
UInt64 part = (UInt64)(i); // << 56;
for (byte bit = 0; bit < 63; bit++)
{
if ((part & LOWBIT_MASK) != 0) // 0x8000000000000000) != 0) //1) != 0) // 0x8000000000000000) != 0)
{
part >>= 1;
part ^= POLY;
}
else
{
part >>= 1;
//currentValue |= 0x8000000000000000;
}
}
part |= TOPBIT_MASK; // 0x8000000000000000;
list.Add(part);
}
return list;
}