int 的最大值

发布于 2024-08-13 20:43:53 字数 84 浏览 7 评论 0原文

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

Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue function in java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寄居者 2024-08-20 20:43:54

在 C++ 中:

#include <limits>

然后使用

int imin = std::numeric_limits<int>::min(); // minimum value
int imax = std::numeric_limits<int>::max();

std::numeric_limits 是一个模板类型,可以用其他类型实例化:

float fmin = std::numeric_limits<float>::min(); // minimum positive value
float fmax = std::numeric_limits<float>::max();

在 C 中:

#include <limits.h>

然后使用

int imin = INT_MIN; // minimum value
int imax = INT_MAX;

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

In 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:

float fmin = std::numeric_limits<float>::min(); // minimum positive value
float fmax = std::numeric_limits<float>::max();

In 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;
跨年 2024-08-20 20:43:54

我知道这是一个老问题,但也许有人可以使用这个解决方案:

int size = 0; // Fill all bits with zero (0)
size = ~size; // Negate all bits, thus all bits are set to one (1)

到目前为止,我们的结果是-1,直到 size 是一个有符号的整数。

size = (unsigned int)size >> 1; // Shift the bits of size one position to the right.

正如标准所说,如果变量有符号且为负,则移入的位为 1;如果变量为无符号或有符号且正,则移入的位为 0。

由于 size 是有符号且为负数,我们将移入符号位 1,这没有多大帮助,因此我们转换为 unsigned int,强制移入 0,将符号位设置为 0,而让所有其他位保持为 1。

cout << size << endl; // Prints out size which is now set to maximum positive value.

我们还可以使用掩码和异或,但随后我们必须知道变量的确切位大小。通过位前移位,我们不必随时知道 int 在机器或编译器上有多少位,也不需要包含额外的库。

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.

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.

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.

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.

以歌曲疗慰 2024-08-20 20:43:54
#include <climits>
#include <iostream>
using namespace std;

int main() {
  cout << INT_MAX << endl;
}
#include <climits>
#include <iostream>
using namespace std;

int main() {
  cout << INT_MAX << endl;
}
萧瑟寒风 2024-08-20 20:43:54

这是我用来获取有符号整数的最大值的宏,该宏与所使用的有符号整数类型的大小无关,并且 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);

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

#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);
太傻旳人生 2024-08-20 20:43:54

好吧,我既没有代表对(Philippe De Muyter)之前的答案发表评论,也没有提高它的分数,因此一个新的例子使用他的 define 为 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;

与使用这个或那个标头不同,这里我们使用编译器的真实类型。

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 define for SIGNED_MAX trivially extended for unsigned types:

// 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.

奢华的一滴泪 2024-08-20 20:43:54

为什么不写一段这样的代码:

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

Why not write a piece of code like:

int  max_neg = ~(1 << 31);
int  all_ones = -1;
int max_pos = all_ones & max_neg;
南烟 2024-08-20 20:43:54
#include <iostrema>

int main(){
    int32_t maxSigned = -1U >> 1;
    cout << maxSigned << '\n';
    return 0;
}

它可能依赖于体系结构,但至少在我的设置中确实有效。

#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文