我执行C中的CRC16算法是:CRC算法实现
init = 0x0000 as long as the data stream goes on if the first bit of data is not equal to the first bit of initial value init = leftshift init once and xor it with the polynomial otherwise leftshift init go to the next data bit init = CRC Checksum
现在的问题是...如果我改变初始化值后的会总是第一个比较等于数据流。
例如:如果我得到的初始值是
1011 0000 1011 0101
和数据流
0011 0110 1000 0101
一次迭代后。
他们会永远是平等的,因为0's
在一开始并不重要,并且可以被忽略。
而下一次迭代后,他们将是:
011 0000 1011 0101
和数据流分别
011 0110 1000 0101
但再次0's
可以忽略不计,我们得到平等对待。
我真的很困惑。
这里是我的C代码:
#define POLY 0x8005
int crc = 0x0000; // Initial value
char data[1024];
int *dp = data;
int fd, nread;
fd = open(argv[1], O_RDWR);
nread = read(fd, data, sizeof(data));
int *end = dp + nread;
while(data < end)
{
crc = crc & 1 && data & 1 ? crc << 1 : (crc << 1)^POLY;
data++;
}
你确定在'&'与'&&'在同一个表达式中混合使用''的优先顺序吗? –
'crc&1 && data&1'是错误的。根据“如果第一位数据不等于初始值的第一位”,它应该是'crc&1 == data&1'。如果你真的想用逻辑运算符来做,它应该是'xor(not)' –
这甚至不会编译。 'data'是一个数组,但是你正在做'data&1'和'data ++'之类的事情。 – interjay