c ++中"unsigned long int"的最大值

use*_*676 7 c++ numeric-limits

如何从"unsigned long int"的类型中知道变量的最大可赋值是多少?

Jer*_*fin 21

显而易见的方法是使用 std::numeric_limits<unsigned long>::max();

  • 哪个有价值...? (2认同)

Jer*_*ner 7

找出的另一种方法是:

unsigned long int i = (unsigned long int) -1;
printf("%lu\n", i);
Run Code Online (Sandbox Code Playgroud)

  • @TemplateRex - `(unsigned long)-1`始终是可以在`unsigned long`中表示的最大值.无论底层硬件如何,无符号算术"都应遵守算术模2n的定律,其中n是整数特定大小的值表示中的位数." [basic.fundamental/4. (4认同)
  • 不适用于1的补码机器(其中-0将是最大值) (3认同)