C++ int 的最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1855459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
maximum value of int
提问by d3vdpro
Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue
function in java?
是否有任何代码可以Integer.MaxValue
在 Java中的 C/C++ 类函数中找到整数的最大值(根据编译器)?
回答by Gregory Pakosz
In C++:
在 C++ 中:
#include <limits>
then use
然后使用
int imin = std::numeric_limits<int>::min(); // minimum value
int imax = std::numeric_limits<int>::max();
std::numeric_limits
is a template type which can be instantiated with other types:
std::numeric_limits
是可以用其他类型实例化的模板类型:
float fmin = std::numeric_limits<float>::min(); // minimum positive value
float fmax = std::numeric_limits<float>::max();
In C:
在 C 中:
#include <limits.h>
then use
然后使用
int imin = INT_MIN; // minimum value
int imax = INT_MAX;
or
或者
#include <float.h>
float fmin = FLT_MIN; // minimum positive value
double dmin = DBL_MIN; // minimum positive value
float fmax = FLT_MAX;
double dmax = DBL_MAX;
回答by MJeB
I know it's an old question but maybe someone can use this solution:
我知道这是一个老问题,但也许有人可以使用这个解决方案:
int size = 0; // Fill all bits with zero (0)
size = ~size; // Negate all bits, thus all bits are set to one (1)
So far we have -1 as result 'till sizeis a signed int.
到目前为止,我们有 -1 作为结果,直到size是一个有符号整数。
size = (unsigned int)size >> 1; // Shift the bits of size one position to the right.
As Standard says, bits that are shifted in are 1 if variable is signed and negative and 0 if variable would be unsigned or signed and positive.
正如标准所说,如果变量有符号且为负,则移入的位为 1,如果变量为无符号或有符号且为正,则移入的位为 0。
As sizeis signed and negative we would shift in sign bit which is 1, which is not helping much, so we cast to unsigned int, forcing to shift in 0 instead, setting the sign bit to 0 while letting all other bits remain 1.
由于大小是有符号和负数,我们会将符号位移入 1,这没有多大帮助,因此我们转换为 unsigned int,强制移入 0,将符号位设置为 0,同时让所有其他位保持为 1。
cout << size << endl; // Prints out size which is now set to maximum positive value.
We could also use a mask and xor but then we had to know the exact bitsize of the variable. With shifting in bits front, we don't have to know at any time how many bits the int has on machine or compiler nor need we include extra libraries.
我们也可以使用掩码和异或,但是我们必须知道变量的确切位大小。通过在位前移位,我们不必随时知道机器或编译器上 int 有多少位,也不需要包含额外的库。
回答by MJeB
#include <climits>
#include <iostream>
using namespace std;
int main() {
cout << INT_MAX << endl;
}
回答by Prabhu
Why not write a piece of code like:
为什么不写一段代码,如:
int max_neg = ~(1 << 31);
int all_ones = -1;
int max_pos = all_ones & max_neg;
回答by Philippe De Muyter
Here is a macro I use to get the maximum value for signed integers, which is independent of the size of the signed integer type used, and for which gcc -Woverflow won't complain
这是我用来获取有符号整数的最大值的宏,它与所使用的有符号整数类型的大小无关,并且 gcc -Woverflow 不会抱怨
#define SIGNED_MAX(x) (~(-1 << (sizeof(x) * 8 - 1)))
int a = SIGNED_MAX(a);
long b = SIGNED_MAX(b);
char c = SIGNED_MAX(c); /* if char is signed for this target */
short d = SIGNED_MAX(d);
long long e = SIGNED_MAX(e);
回答by A. Genchev
O.K. I neither have rep to comment on previous answer (of Philippe De Muyter) nor raise it's score, hence a new example using his definefor SIGNED_MAX trivially extended for unsigned types:
好的,我既没有代表对先前的答案(Philippe De Muyter 的)发表评论,也没有提高它的分数,因此使用他对 SIGNED_MAX 的定义的新示例对无符号类型进行了简单扩展:
// We can use it to define limits based on actual compiler built-in types also:
#define INT_MAX SIGNED_MAX(int)
// based on the above, we can extend it for unsigned types also:
#define UNSIGNED_MAX(x) ( (SIGNED_MAX(x)<<1) | 1 ) // We reuse SIGNED_MAX
#define UINT_MAX UNSIGNED_MAX(unsigned int) // on ARM: 4294967295
// then we can have:
unsigned int width = UINT_MAX;
Unlike using this or that header, here we use the real type from the compiler.
与使用这个或那个头文件不同,这里我们使用来自编译器的真实类型。
回答by macmur
#include <iostrema>
int main(){
int32_t maxSigned = -1U >> 1;
cout << maxSigned << '\n';
return 0;
}
It might be architecture dependent but it does work at least in my setup.
它可能与架构有关,但至少在我的设置中确实有效。
回答by Hao
For the specific maximum value of int, I usually write the hexadecimal notation:
对于int的具体最大值,我通常写成十六进制表示法:
int my_max_int = 0x7fffffff;
instead of the irregular decimal value:
而不是不规则的十进制值:
int my_max_int = 2147483647;
回答by izanbf1803
What about (1 << (8*sizeof(int)-2)) - 1 + (1 << (8*sizeof(int)-2))
.
This is the same as 2^(8*sizeof(int)-2) - 1 + 2^(8*sizeof(int)-2)
.
怎么样(1 << (8*sizeof(int)-2)) - 1 + (1 << (8*sizeof(int)-2))
。这与2^(8*sizeof(int)-2) - 1 + 2^(8*sizeof(int)-2)
.
If sizeof(int) = 4 => 2^(8*4-2) - 1 + 2^(8*4-2) = 2^30 - 1 + 20^30 = (2^32)/2 - 1 [max signed int of 4 bytes]
.
如果sizeof(int) = 4 => 2^(8*4-2) - 1 + 2^(8*4-2) = 2^30 - 1 + 20^30 = (2^32)/2 - 1 [max signed int of 4 bytes]
.
You can't use 2*(1 << (8*sizeof(int)-2)) - 1
because it will overflow, but (1 << (8*sizeof(int)-2)) - 1 + (1 << (8*sizeof(int)-2))
works.
你不能使用,2*(1 << (8*sizeof(int)-2)) - 1
因为它会溢出,但(1 << (8*sizeof(int)-2)) - 1 + (1 << (8*sizeof(int)-2))
有效。