shift count is too large

STM8S是8 bit单片机
在STM8S中 unsigned long是32位, unsigned short和unsigned int都是16位,unsigned char是8位。

以以下代码编译时报了“shift count is too large”的warning, 并且代码执行时也没有达到预期效果:

1 unsigned char temp[4];
2 unsigned long pd_reg;
3 ...
4 pd_reg = temp[3] << 24 | temp[2] << 16 | temp[1] << 8 | temp[0];

后面加上了强制类型转换解决了此问题:

1 unsigned char temp[4];
2 unsigned long pd_reg;
3 ...
4 pd_reg = (unsigned long)temp[3] << 24 | (unsigned long)temp[2] << 16 | (unsigned long)temp[1] << 8 | (unsigned long)temp[0];

 

posted @ 2020-03-05 15:58  闹闹爸爸  阅读(5560)  评论(0编辑  收藏  举报