简体   繁体   English

int的最大值

[英]maximum value of int

是否有任何代码可以在C / C ++中找到整数的最大值(相应于编译器),如java中的Integer.MaxValue函数?

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;

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 size is a signed int. 到目前为止,我们的结果为-1,直到size为signed int。

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 size is 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,这没有多大帮助,因此我们转换为无符号整数,强制转换为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. 我们也可以使用掩码和xor但是我们必须知道变量的确切位数。 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在机器或编译器上有多少位,也不需要包含额外的库。

#include <climits>
#include <iostream>
using namespace std;

int main() {
  cout << INT_MAX << endl;
}

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);

Why not write a piece of code like: 为什么不写一段代码,如:

int  max_neg = ~(1 << 31);
int  all_ones = -1;
int max_pos = all_ones & max_neg;

OK I neither have rep to comment on previous answer (of Philippe De Muyter) nor raise it's score, hence a new example using his define for 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. 与使用这个或那个标头不同,这里我们使用编译器中的实际类型。

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;

What about (1 << (8*sizeof(int)-2)) - 1 + (1 << (8*sizeof(int)-2)) . 那么(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) . 这与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))有效。

#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. 它可能依赖于体系结构,但它至少在我的设置中起作用。

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

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