简体   繁体   English

我需要了解这个 cpp 代码背后的逻辑

[英]I need to understand the logic behind this cpp code

int x = 25;
unsigned int g = x & 0x80000000;

how did this code read the most significant bit of in the address of x?这段代码是如何读取 x 的地址中的最高有效位的? does the reference to 0x80000000, or binary 1000 0000 0000 0000 accomplished that task, or was it something else?对 0x80000000 或二进制 1000 0000 0000 0000 的引用是否完成了该任务,还是其他什么?

For char the most significant bit is typically the sign bit as per Two's Complement , so this should be:对于char最重要的位通常是根据Two's Complement的符号位,所以这应该是:

 char x = 25;
 unsigned int msb = x & (1 << 6);

Where (1 << 6) means the 6 bit, counting from 0, or the 7th counting from 1st.其中(1 << 6)表示第 6 位,从 0 开始计数,或从 1 开始计数的第 7 位。 It's the second-to-top bit and equivalent to 0x40 .它是0x40第二位,相当于0x40

Since 25 is 0b00011001 you won't get a bit set.由于250b00011001您不会得到任何设置。 You'll need a value >= 64.您需要一个 >= 64 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM