(long long)有什么真正的下限?

6

使用以下代码检查 long long 类型的限制:

std::cout << std::numeric_limits<long long>::min();

我得到了-9223372036854775808。

然而,当编译以下代码时:

int main() {
   long long l;
   l=-9223372036854775808LL;
}

我收到了警告信息:

test.cpp:3:7: warning: integer constant is so large that it is unsigned.
test.cpp:3: warning: this decimal constant is unsigned only in ISO C90

我错过了什么吗?非常感谢您的帮助。Giorgio
3个回答

13

这个数 9223372036854775808LL 是一个正数。 所以你需要进行处理。

std::cout << std::numeric_limits<long long>::max();

考虑到这一点,紧接着立即否定它并不会使-运算符的操作数本身变为负数。


4
关于为什么您的系统中最小值和最大值不完全对称于零,您可能需要阅读有关 二进制补码 的内容。 - Johannes Schaub - litb

1

使用std::numeric和boost::numeric都可以正常工作,没有任何警告。

#include <iostream>
#include <boost/numeric/conversion/bounds.hpp>
#include <boost/limits.hpp>

int main(int argc, char* argv[]) {

  std::cout << "The minimum value for long long:\n";
  std::cout << boost::numeric::bounds<long long>::lowest() << std::endl;
  std::cout << std::numeric_limits<long long>::min() << std::endl << std::endl;

  std::cout << "The maximum value for long long:\n";
  std::cout << boost::numeric::bounds<long long>::highest() << std::endl;
  std::cout << std::numeric_limits<long long>::max() << std::endl << std::endl;

  std::cout << "The smallest positive value for long long:\n";
  std::cout << boost::numeric::bounds<long long>::smallest() << std::endl << std::endl;


  long long l;
  l = boost::numeric::bounds<long long>::lowest();
  std::cout << l << std::endl;
  l = std::numeric_limits<long long>::min();
  std::cout << l << std::endl;


  return 0;
}

0

在我的系统中。

   {LLONG_MIN}
          Minimum value of type long long.
          Maximum Acceptable Value: -9223372036854775807

   {LLONG_MAX}
          Maximum value of type long long.
          Minimum Acceptable Value: +9223372036854775807

请查看您的limits.h文件

此外,也可以通过以下方式找到:

min:- 2^(sizeof (long long) - 1) - 1

max:+ 2^(sizeof (long long) - 1)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接