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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:51:15  来源:igfitidea点击:

Maximum value for unsigned int

c++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

C

C

#include <limits.h>
unsigned int max_unsigned_int_size = UINT_MAX;

C++

C++

#include <limits>
unsigned int max_unsigned_int_size = std::numeric_limits<unsigned int>::max();

回答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, sizeofwould 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

  • For C the value is given by UINT_MAXin limits.h.
  • For C++ you can alternatively use std::numeric_limits<unsigned int>::max()from limits.
  • 对于 C,值由UINT_MAXin给出limits.h
  • 对于 C++,您也可以使用std::numeric_limits<unsigned int>::max()from limits

回答by Remy Lebeau

#include <limits>

max_unsigned_int_size = std::numeric_limits<unsigned int>::max();