简体   繁体   English

16 位 -32768 到 32767 与 32 位与 64 位的范围 Integer -2147483648 到 2147483647

[英]Range of 16 bit -32768 to 32767 vs 32 bit vs 64 bit Integer -2147483648 to 2147483647

I've seen lots of places say:我看到很多地方说:

In Turbo c and 16-bit range of Integer is -32768 to 32767 (0 to 65535) but in 32-bit and 64-bit range of Integer is -2147483648 [minimum value -2^31] to 2147483647 [maximum value 2^31-1] . In Turbo c and 16-bit range of Integer is -32768 to 32767 (0 to 65535) but in 32-bit and 64-bit range of Integer is -2147483648 [minimum value -2^31] to 2147483647 [maximum value 2^ 31-1] But when we code and try to overflow the integer range like x=2147483649 c compiler returned x=-2147483647.但是当我们编写代码并尝试溢出integer 范围时,例如 x=2147483649 c 编译器返回 x=-2147483647。

When we give (+ve) overflow value in x variable it returns (-ve) value or vise versa.当我们在 x 变量中给出 (+ve) 溢出值时,它返回 (-ve) 值,反之亦然。 And when we enter (+/-ve)2147483648 it always give (-ve) 2147483648 WHY ??当我们输入 (+/-ve)2147483648 时,它总是给出 (-ve) 2147483648为什么

Example Code for better explanation示例代码以获得更好的解释

#include<stdio.h>

int main(){

    int x;

    x=2147483649;

    printf("%d",x);
    
    // if we give x=2147483649
    // Output will be -2147483647

    // if we give x=-2147483649
    // output will be +2147483647

    // if we give x=(+/-)2147483648
    // output will ve always -ve 2147483648

    return 0;

}

In an assignment such as x = 2147483649 , the value on the right is converted to the type of the left operand.x = 2147483649等赋值中,右侧的值将转换为左侧操作数的类型。 When converting to a signed integer type, if the source value cannot be represented in the destination type, the result of the conversion is defined by the implementation (largely by the compiler), per C 2018 6.3.1.3 3.转换为带符号的 integer 类型时,如果源值无法在目标类型中表示,则转换结果由实现定义(主要由编译器),根据 C 2018 6.3.1.3 3。

GCC defines the conversion to wrap modulo 2 N , where N is the number of bits in the type 1 , and this is a common behavior among compilers. GCC 定义了模 2 N的转换,其中N是类型1的位数,这是编译器中的常见行为。

Wrapping 2,147,483,649 modulo 2 32 produces 2,147,483,649 − 4,294,967,296 = −2,147,483,647.包装 2,147,483,649 模 2 32产生 2,147,483,649 - 4,294,967,296 = -2,147,483,647。

This is not an overflow.这不是溢出。 Overflow, or, more generally, an exceptional condition , occurs when the result of an operation is not representable in the result type of the operation (C 2018 6.5 5).当操作的结果无法在操作的结果类型中表示时,就会发生溢出,或者更一般地说,异常情况(C 2018 6.5 5)。 Since the conversion is defined to produce a result that is representable, no overflow occurs.由于转换被定义为产生可表示的结果,因此不会发生溢出。

Footnote脚注

1 The number of value bits and the sign bit, so 32 in this case. 1值位和符号位的数量,在这种情况下为 32。 Padding bits are not included.不包括填充位。

It helps if you look at the bit patterns of all those numbers.如果您查看所有这些数字的位模式,它会有所帮助。 32 bits is 8 hex digits, anything longer than that gets truncated on the left: 32 位是 8 个十六进制数字,任何比它长的都会在左边被截断:

               32 bit       64 bit
2147483649:  0x80000001  0x0000000080000001
-2147483647: 0x80000001  0xffffffff80000001
-2147483649: 0x7fffffff  0xffffffff7fffffff
2147483647:  0x7fffffff  0x000000007fffffff
2147483648:  0x80000000  0x0000000080000000
-2147483648: 0x80000000  0xffffffff80000000

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

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