C++ unsigned int 的最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15889253/
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 for unsigned int
提问by Evan Hahn
Here's what I want:
这是我想要的:
unsigned int max_unsigned_int_size;
max_unsigned_int_size = ???;
How should I do this?
我该怎么做?
回答by John Kugelman
回答by Jens Gustedt
unsigned int max_unsigned_int_size = -1;
is guaranteed to do the right thing. Arithmetic with unsigned types is always modulo.
保证做正确的事。无符号类型的算术总是取模的。
But in the concrete case you always should use UINT_MAX
但在具体情况下,你总是应该使用 UINT_MAX
回答by sehe
You're looking for
您正在寻找
#include <limits>
std::numeric_limits<unsigned int>::max();
If you wanted the size, sizeof
would do, multiply by CHAR_BITS to get the bits.
如果你想要大小,sizeof
可以做,乘以 CHAR_BITS 得到位。
Alternattively, there is
或者,有
std::numeric_limits<unsigned int>::digits();
回答by juanchopanza
You need std::numeric_limits::max()
你需要 std::numeric_limits::max()
#include <limits>
...
max_insigned_int_size = std::numeric_limits<unsigned int>::max():
回答by David Heffernan
回答by Remy Lebeau
#include <limits>
max_unsigned_int_size = std::numeric_limits<unsigned int>::max();