gpt4 book ai didi

c - C中~0的值是多少?

转载 作者:太空狗 更新时间:2023-10-29 16:50:55 25 4
gpt4 key购买 nike

我想获取 INT_MININT_MAX 的值。我已经尝试了 ~0~0 >> 1 因为最左边的位是符号位但是我得到了 -1 它们两个.

很困惑,为什么~0不是0xffffffff~0 >> 1 0x7fffffff?

最佳答案

使用:

~0U >> 1

无符号移位行为的后缀“U”。

so, confused that why not ~0 turns out to be 0xffffffff?

看看,什么是0用四个字节表示:

BIT NUMBER    31                                     0
▼ ▼
number bits 0000 0000 0000 0000 0000 0000 0000 0000
▲ ▲
MSB LSB



LSB - Least Significant Bit (numbered 0)
MSB - Most Significant Bit (numbered 31)

现在~是按位非运算符然后翻转 0 中的所有位作为:

BIT NUMBER    31                                     0
▼ ▼
number bits 1111 1111 1111 1111 1111 1111 1111 1111
▲ ▲
MSB LSB

因为 MSB = 1此表示被视为负数,其大小是使用 2' 补码数学找到的,即 -1 .

如何?

什么是 1 ?它是:

number bits    0000 0000 0000 0000 0000 0000 0000 0001 
▲ ▲
MSB LSB

1 的 1 补码

number bits    1111 1111 1111 1111 1111 1111 1111 1110
▲ ▲
MSB LSB

2'补码?添加1补语,即:

number bits    1111 1111 1111 1111 1111 1111 1111 1111
▲ ▲
MSB LSB

这与您获得 ~0 时相同?这就是为什么你得到 -1输出。

现在 >> 移位运算符?

在大多数 C 实现中,>> 运算符被定义为算术右移,它保留符号位 MSB。所以~0 >> 1注意到但是-1保持不变。

6.5.7 [Bitwise shift operators]

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

你的要求就是所谓的无符号右移>>并且可以使用无符号数找到您需要的行为,这就是为什么我在 U 后缀作为0U .

如何打印INT_MIN和INT_MAX?

因为在 C 中打印 INT_MIN 和 INT_MAX 有点棘手(因为设置 MSB 和位溢出的未定义和实现行为)所以我编写了如下代码:

#include <stdio.h>
#include<limits.h> /* include for CHAR_BIT */
int main(){
int my_int_min = 1U << ((sizeof(int) * CHAR_BIT) - 1);
int my_int_max = ~0U >> 1;
printf("INT_MIN = %d\n", my_int_min);
printf("INT_MAX = %d\n", my_int_max);
return 0;
}

看到它执行@ codepad ,它的输出是:

INT_MIN  = -2147483648
INT_MAX = 2147483647

这段代码是如何工作的?

注意 32 位数字范围是 [-2147483648, 2147483647]等于 [-2<sup>31</sup>, 2<sup>31</sup> -1 ] .

INT_MIN: -231 == -2147483648 是:

    1000 0000 0000 0000 0000 0000 0000 0000 
▲ ▲
MSB LSB

在表达式中1U << ((sizeof(int) * CHAR_BIT) - 1) ,我将 LSB(即 1)的第一位移动到 MSB 的最左侧,并且因为在 C 中,设置有符号位是未定义的行为当操作数是单数类型时所以我使用了无符号的 1U。

6.5.7 [Bitwise shift operators]

The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

另一点要注意的是,我使用了 CHAR_BIT,这是一个在 limits.h 中定义的标准宏,它告诉 C 实现中一个字符中的位数(记住:一个字符总是一个字节大小,但一个字节中的位数可以是不同系统上的不同并不总是保证为 8)。

INT_MAX: 231 -1 == 2147483647

    0111 1111 1111 1111 1111 1111 1111 1111
▲ ▲
MSB LSB

关于c - C中~0的值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180157/

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