C 将奇数结果左移超过 15 位
所以我将一个字符向上移动到一个长字符,然后清除,然后再做一次。 15 的转变使我的机器变得古怪,请参阅代码中的注释。您认为是什么造成了这种情况。
本机char 0x00,long 0x0000 0000
unsigned char temp;
unsigned long open_code;
temp = 0x01;
open_code = open_code | (temp <<1);// open_code = 0x0000 0002
open_code = 0;
//......
// 2 - 7
//....
temp = 0x01;
open_code = open_code | (temp <<8); // open_code = 0x0000 0100
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<9); // open_code = 0x0000 0200
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<10); // open_code = 0x0000 0400
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<11); // open_code = 0x0000 0800
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<12); // open_code = 0x0000 1000
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<13); // open_code = 0x0000 2000
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<14); // open_code = 0x0000 4000
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<15); // open_code = 0xFFFF 8000 !!!!!
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<16); // open_code = 0x0000 0000 !!!!!
so I am shifting up a char into a long, then clearing, and doing it again. A shift of 15 makes my machine go wacky, see comment in code. What do you think is causing this.
This machine char 0x00, and long 0x0000 0000
unsigned char temp;
unsigned long open_code;
temp = 0x01;
open_code = open_code | (temp <<1);// open_code = 0x0000 0002
open_code = 0;
//......
// 2 - 7
//....
temp = 0x01;
open_code = open_code | (temp <<8); // open_code = 0x0000 0100
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<9); // open_code = 0x0000 0200
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<10); // open_code = 0x0000 0400
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<11); // open_code = 0x0000 0800
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<12); // open_code = 0x0000 1000
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<13); // open_code = 0x0000 2000
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<14); // open_code = 0x0000 4000
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<15); // open_code = 0xFFFF 8000 !!!!!
open_code = 0;
temp = 0x01;
open_code = open_code | (temp <<16); // open_code = 0x0000 0000 !!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您将
unsigned char
移动得太远,从而导致溢出。尝试在转换之前将其转换为long
,如下所示:It looks like you're shifting an
unsigned char
too far and as a result inducing overflow. Try casting it to along
before shifting, like so: