gpt4 book ai didi

c - macOS 10.12 中 C 语言的位右移与 Ubuntu 16.04 不同

转载 作者:行者123 更新时间:2023-11-30 20:03:27 25 4
gpt4 key购买 nike

更新:我刚刚在 macOS 上的新 C 源文件中重写了该函数:

#include <stdio.h>

int main() {

int x = 0xffffffff;

int m2 = (((((0x55 << 8) + 0x55 )<< 8) + 0x55)<< 8) + 0x55;
printf("m2 : 0x%x\n",m2);

int m4 = (((((0x33 << 8) + 0x33 )<< 8) + 0x33)<< 8) + 0x33;
printf("m4 : 0x%x\n",m4);

int m8 = (((((0x0f << 8) + 0x0f )<< 8) + 0x0f)<< 8) + 0x0f;
printf("m8 : 0x%x\n",m8);

int m16 = (0xff << 16) + 0xff ;
printf("m16 : 0x%x\n",m16);

int p1 = (x & m2) + ((x >> 1) & m2);
printf("p1: 0x%x\n",p1);

printf("p1 & m4 : 0x%x\n",p1 & m4);

printf("p1 >> 2 : 0x%x\n",p1 >> 2);

printf("(p1 >> 2) & m4 : 0x%x\n",(p1 >> 2) & m4);

int p2 = (p1 & m4) + ((p1 >> 2) & m4);
printf("p2 : 0x%x\n",p2);

int p3 = (p2 & m8) + ((p2 >> 4) & m8) ;
printf("p3 : 0x%x\n",p3);

int p4 = (p3 & m16) + ((p3 >> 8) & m16) ;
printf("p4 : 0x%x\n",p4);
//int p4 = p3 + (p3 >> 8) ;
int p5 = p4 + (p4 >> 16) ;

printf("BigCount result is : 0x%x\n",p5 & 0xFF);
}

打印的结果是: enter image description here

一切都和Ubuntu中的一样。这让我更加困惑。

<小时/>

当我在 macOS 10.12 中运行此函数时,它给出了意想不到的答案。输入 x0xffffffff (-1)。该函数用C语言编写:

int bitCount(int x) {

int m2 = (((((0x55 << 8) + 0x55 )<< 8) + 0x55)<< 8) + 0x55;
int m4 = (((((0x33 << 8) + 0x33 )<< 8) + 0x33)<< 8) + 0x33;
int m8 = (((((0x0f << 8) + 0x0f )<< 8) + 0x0f)<< 8) + 0x0f;
int m16 = (0xff << 16) + 0xff ;

int p1 = (x & m2) + ((x >> 1) & m2);
int p2 = (p1 & m4) + ((p1 >> 2) & m4); //line 7
int p3 = (p2 & m8) + ((p2 >> 4) & m8) ;
int p4 = (p3 & m16) + ((p3 >> 8) & m16) ;
//int p4 = p3 + (p3 >> 8) ;
int p5 = p4 + (p4 >> 16) ;

return p5 & 0xFF;
}

当我通过打印来跟踪所有局部变量时,我发现:

((p1 >> 2) & m4) //line7

打印“0x2222222”的值(七个“2”而不是八个“2”)。 enter image description here

这太出乎意料了,p1打印'0x2aaaaaaa'而m4是'0x33333333',所以它应该是0x22222222(八个'2;)。但是,当我在 ubuntu 16.04 中运行此命令时,一切都如预期,因为 ((p1 >> 2) & m4) 打印“0x22222222”: enter image description here

您在 Mac 上运行此程序时是否遇到同样的问题? macOS 中是否有什么不同导致了这个问题?

最佳答案

6.5.7 Bitwise shift operators ,C 标准的(注意突出显示的部分):

Constraints

2 Each of the operands shall have integer type.

Semantics

3 The integer promotions are performed on each of the operands. Thetype of the result is that of the promoted left operand. If the valueof the right operand is negative or is greater than or equal to thewidth of the promoted left operand, the behavior is undefined.

4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacatedbits are filled with zeros. If E1 has an unsigned type, the value ofthe result is E1 x 2E2 , reduced modulo one more than the maximumvalue representable in the result type. If E1 has a signed type andnonnegative value, and E1 x 2E2 is representable in the result type,then that is the resulting value; otherwise, the behavior isundefined.

5 The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1has an unsigned type or if E1 has a signed type and a nonnegativevalue, the value of the result is the integral part of the quotient ofE1 / 2E2 . If E1 has a signed type and a negative value, theresulting value is implementation-defined.

您正在对有符号整数值进行位移。根据您的输入,您将依赖于未定义的行为和实现定义的行为。

不同平台上的不同结果是可以预料的。

关于c - macOS 10.12 中 C 语言的位右移与 Ubuntu 16.04 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com