理解语法:crc ^= *((unsigned char *)memptr)++;
我想了解这个语法来修复它的 crc16 函数:
unsigned short crc16(void *memptr, int len) {
int j;
unsigned short crc = CRC16_INIT ;
while(len--){
crc ^= *((unsigned char *)memptr)++;
for(j=0; j<8; j++){
if(crc & 1)
crc =(USHORT)( (crc>>1) ^ CRC16_POLY );
else
crc =(USHORT)( crc>>1);
}//for
}//while
return crc ;
}
它是旧编译器上的工作代码,现在我已经有了
错误:IntelliSense:表达式必须是可修改的左值
:
crc ^= *((unsigned char *)memptr)++;
编译器错误:错误 C2105:“++”需要左值
重新编码(希望它是正确的):
unsigned char oldValue = *((unsigned char *)memptr);
++*((unsigned char *)memptr);
crc ^= oldValue; // <--- WRONG
crc ^= (*((unsigned char *)memptr))++; // <--- WRONG
I want to understand this syntax to fix it for crc16 function :
unsigned short crc16(void *memptr, int len) {
int j;
unsigned short crc = CRC16_INIT ;
while(len--){
crc ^= *((unsigned char *)memptr)++;
for(j=0; j<8; j++){
if(crc & 1)
crc =(USHORT)( (crc>>1) ^ CRC16_POLY );
else
crc =(USHORT)( crc>>1);
}//for
}//while
return crc ;
}
it was working code on older compiler and now I 've got
error : IntelliSense: expression must be a modifiable lvalue
on this line:
crc ^= *((unsigned char *)memptr)++;
Compiler error : error C2105: '++' needs l-value
Recoded to it (hope it's correct):
unsigned char oldValue = *((unsigned char *)memptr);
++*((unsigned char *)memptr);
crc ^= oldValue; // <--- WRONG
crc ^= (*((unsigned char *)memptr))++; // <--- WRONG
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将上面的行更改为:
您的代码不起作用,因为
((unsigned char *)memptr)
给出了无法递增的右值。你的旧编译器有错误。Change the above line to this:
Your code doesn't work because
((unsigned char *)memptr)
gives a rvalue which cannot be incremented. Your old compiler had bug.该函数的 ASM 版本(没有优化,但仍然是 0x59 字节,而我的编译器的 C++ 版本是 0x69)。
如果您熟悉 ASM,那么理解上述 C++ 代码段和整个函数就足够了。
ASM-version of this function (not that optimized, but still 0x59 bytes vs my compiler's 0x69 of C++ version).
If you are familiar with ASM, it will be enough to understand mentioned C++ code segment and the whole function overall.
打破该行:
(unsigned char *)memptr
- 这会将void*
(参数)转换为unsigned char*
+ +
- 后增量,即增量并返回先前的值(即指针增量 - 注意:这是本地副本)*()
- this取消引用指针先前值的内容(所以现在我们有一个unsigned char
)^=
xor 与当前值crc
正如 ZeRemz 在他的评论中提到的,运算符的顺序此处应用的是产生此错误的原因。
要解决此问题,您需要了解初衷是什么(并修复)
更新指针并异或先前的值
unsigned char* t = reinterpret_cast(memptr);
..
crc ^= *(t++);
更新字符的值并使用它之前的值(尽管这对于生成 CRC 没有多大意义)
crc ^= (*reinterpret_cast(memptr))++;
Break that line up:
(unsigned char *)memptr
- this casts thevoid*
(argument) to anunsigned char*
++
- post increment, i.e. increment and return the previous value (i.e. the pointer is incremented - note: this is a local copy)*()
- this dereferences the contents of the previous value of pointer (so now we have anunsigned char
)^=
xor with current value ofcrc
As ZeRemz mentions in his comment, the order of the operators applied here is what is generating this error.
To fix, this you need to understand what the original intention is (and fixes)
Update the pointer and xor the previous value
unsigned char* t = reinterpret_cast<unsigned char*>(memptr);
..
crc ^= *(t++);
Update the value of the character and use it's previous value (though this does not make much sense for generating a CRC)
crc ^= (*reinterpret_cast<unsigned char *>(memptr))++;