整数类型存储在C ++中的值的范围 [英] What range of values can integer types store in C++

查看:120
本文介绍了整数类型存储在C ++中的值的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在32位计算机上可以 unsigned long int 保存十位数字(1,000,000,000 - 9,999,999,999)。

unsigned long int long int unsigned int short int short unsigned int int

解决方案

您可以依赖的最低范围是:




  • short int int :-32,767 to 32,767

  • unsigned short int unsigned int :0至65,535

  • long int :-2,147,483,647至2,147,483,647

  • unsigned long int 0到4,294,967,295



这意味着没有, long int 可存储任何10位数字。但是,一个更大的类型 long long int 被引入C中的C和C ++ 11中的C ++(这种类型也经常被编译器支持作为扩展,不包括它)。如果您的编译器支持,此类型的最小范围是:




  • long long int :-9,223,372,036,854,775,807至9,223,372,036,854,775,807

  • unsigned long long int :0至18,446,744,073,709,551,615



    • 这样类型就足够了(再次,如果你有它)。



      < hr>

      对于那些相信我已经犯了这些下限的人,我有一个注释 - 我没有。写入范围的C要求以允许1的补码或符号大小的整数表示,其中最低可表示值和最高可表示值仅在符号上不同。还允许具有二进制补码表示,其中符号位1和所有值位0的值是陷阱表示而不是合法值。换句话说, int 不是需要能够表示值-32,768。


      Can unsigned long int hold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer.
      What are the ranges of unsigned long int , long int, unsigned int, short int, short unsigned int and int?

      解决方案

      The minimum ranges you can rely on are:

      • short int and int: -32,767 to 32,767
      • unsigned short int and unsigned int: 0 to 65,535
      • long int: -2,147,483,647 to 2,147,483,647
      • unsigned long int: 0 to 4,294,967,295

      This means that no, long int cannot be relied upon to store any 10 digit number. However, a larger type long long int was introduced to C in C99 and C++ in C++11 (this type is also often supported as an extension by compilers built for older standards that did not include it). The minimum range for this type, if your compiler supports it, is:

      • long long int: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
      • unsigned long long int: 0 to 18,446,744,073,709,551,615

      So that type will be big enough (again, if you have it available).


      A note for those who believe I've made a mistake with these lower bounds - I haven't. The C requirements for the ranges are written to allow for ones' complement or sign-magnitude integer representations, where the lowest representable value and the highest representable value differ only in sign. It is also allowed to have a two's complement representation where the value with sign bit 1 and all value bits 0 is a trap representation rather than a legal value. In other words, int is not required to be able to represent the value -32,768.

      这篇关于整数类型存储在C ++中的值的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆