可以unsigned long int在32位计算机上保存十位数字(1,000,000,000 - 9,999,999,999)吗?
此外,有什么的范围unsigned long int,long int,unsigned int,short int,short unsigned int,和int?
caf*_*caf 118
您可以信赖的最小范围是:
short int和int:-32,767至32,767unsigned short int并且unsigned int:0到65,535long int:-2,147,483,647至2,147,483,647unsigned long int:0到4,294,967,295这意味着不能,long int 不能依赖于存储任何10位数字.但是,long long intC99中的C和C++ 11中的C++引入了更大的类型(这种类型也经常作为扩展,由不包含它的旧标准构建的编译器支持).如果您的编译器支持,此类型的最小范围是:
long long int:-9,223,372,036,854,775,807至9,223,372,036,854,775,807unsigned long long int:0到18,446,744,073,709,551,615所以这种类型将足够大(再次,如果你有它可用).
对于那些认为我在这些下限上犯了错误的人的说明 - 我没有.编写范围的C要求是为了允许1的补码或符号幅度整数表示,其中最低可表示值和最高可表示值仅在符号上不同.它还允许具有二进制补码表示,其中符号位1和所有值位0的值是陷阱表示而不是合法值.换言之,int是不要求是能够代表值-32,768.
Yac*_*oby 29
数字类型的大小没有在C++标准中定义,尽管最小大小是.告诉您平台大小的方法是使用数字限制
例如,int的最大值可以通过以下方式找到:
std::numeric_limits<int>::max();
Run Code Online (Sandbox Code Playgroud)
计算机在基数10中不起作用,这意味着最大值将以2 n -1 的形式出现,因为内存中的表示数如何.以八位(1字节)为例
0100 1000
Run Code Online (Sandbox Code Playgroud)
当设置为1时,最右边的位(数字)表示2 0,下一个位2 1,然后是2 2,依此类推,直到我们到达最左边的位,如果数字是无符号则表示2 7.
所以数字表示2 6 + 2 3 = 64 + 8 = 72,因为右边的第4位和左边的第7位被设置.
如果我们将所有值设置为1:
11111111
Run Code Online (Sandbox Code Playgroud)
这个数字现在(假设无符号)
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 = 2 8 - 1
我们可以看到,这是可以用8位表示的最大可能值.
在我的机器和int和long是相同的,每个能够保持在-2 31到2 31 - 1.根据我的经验,在现代32位台式机上最常见的大小.
为了找出在限制你的系统:
#include <iostream>
#include <limits>
int main(int, char **) {
std::cout
<< static_cast< int >(std::numeric_limits< char >::max()) << "\n"
<< static_cast< int >(std::numeric_limits< unsigned char >::max()) << "\n"
<< std::numeric_limits< short >::max() << "\n"
<< std::numeric_limits< unsigned short >::max() << "\n"
<< std::numeric_limits< int >::max() << "\n"
<< std::numeric_limits< unsigned int >::max() << "\n"
<< std::numeric_limits< long >::max() << "\n"
<< std::numeric_limits< unsigned long >::max() << "\n"
<< std::numeric_limits< long long >::max() << "\n"
<< std::numeric_limits< unsigned long long >::max() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
请注意,这long long仅适用于C99和C++ 11.
其他人会发布data_sizes和精度等的链接.
我将告诉你如何自己解决这个问题.
写一个小应用程序,将执行以下操作.
unsigned int ui;
std::cout << sizeof(ui));
Run Code Online (Sandbox Code Playgroud)
这将(取决于编译器和archicture)打印2,4或8,说2个字节长,4个字节长等.
让我们假设它是4.
您现在想要存储的最大值为4个字节,一个字节的最大值为(十六进制)0xFF.四个字节的最大值是0x,后跟8个f(每个字节一对f,0x告诉编译器后面的字符串是十六进制数).现在更改程序以分配该值并打印结果
unsigned int ui = 0xFFFFFFFF;
std::cout << ui;
Run Code Online (Sandbox Code Playgroud)
这是unsigned int可以容纳的最大值,如基数10表示所示.
现在为长期,短裤和任何其他您感兴趣的INTEGER值做这件事.
注意:这种方法不适用于浮点数(即double或float).
希望这可以帮助
在C++中,现在使用2的compliment方法存储int和其他数据.这意味着范围是:
-2147483648 to 2147483647
Run Code Online (Sandbox Code Playgroud)
或-2 ^ 31至2 ^ 31-1
1位保留为0,因此正值小于2 ^(31)
您可以使用头文件中的numeric_limits<data_type>::min()和numeric_limits<data_type>::max()函数limits并找到每种数据类型的限制。
#include <iostream>
#include <limits>
using namespace std;
int main()
{
cout<<"Limits of Data types:\n";
cout<<"char\t\t\t: "<<static_cast<int>(numeric_limits<char>::min())<<" to "<<static_cast<int>(numeric_limits<char>::max())<<endl;
cout<<"unsigned char\t\t: "<<static_cast<int>(numeric_limits<unsigned char>::min())<<" to "<<static_cast<int>(numeric_limits<unsigned char>::max())<<endl;
cout<<"short\t\t\t: "<<numeric_limits<short>::min()<<" to "<<numeric_limits<short>::max()<<endl;
cout<<"unsigned short\t\t: "<<numeric_limits<unsigned short>::min()<<" to "<<numeric_limits<unsigned short>::max()<<endl;
cout<<"int\t\t\t: "<<numeric_limits<int>::min()<<" to "<<numeric_limits<int>::max()<<endl;
cout<<"unsigned int\t\t: "<<numeric_limits<unsigned int>::min()<<" to "<<numeric_limits<unsigned int>::max()<<endl;
cout<<"long\t\t\t: "<<numeric_limits<long>::min()<<" to "<<numeric_limits<long>::max()<<endl;
cout<<"unsigned long\t\t: "<<numeric_limits<unsigned long>::min()<<" to "<<numeric_limits<unsigned long>::max()<<endl;
cout<<"long long\t\t: "<<numeric_limits<long long>::min()<<" to "<<numeric_limits<long long>::max()<<endl;
cout<<"unsiged long long\t: "<<numeric_limits<unsigned long long>::min()<<" to "<<numeric_limits<unsigned long long>::max()<<endl;
cout<<"float\t\t\t: "<<numeric_limits<float>::min()<<" to "<<numeric_limits<float>::max()<<endl;
cout<<"double\t\t\t: "<<numeric_limits<double>::min()<<" to "<<numeric_limits<double>::max()<<endl;
cout<<"long double\t\t: "<<numeric_limits<long double>::min()<<" to "<<numeric_limits<long double>::max()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出将是: 数据类型的限制: