Giorgio A
Giorgio A

Reputation: 63

What's the real lower limit for (signed) long long?

Checking the limits of the type long long using

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

I get -9223372036854775808

However when compiling the following code:

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

I get the warnings:

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

What am I missing? Many thanks in advance for your help.

Giorgio

Upvotes: 6

Views: 2570

Answers (3)

Arlen
Arlen

Reputation: 6835

It works fine with std::numeric and boost::numeric; it gives no warnings.

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

Upvotes: 1

phoxis
phoxis

Reputation: 61940

In my system.

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

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

see your limits.h file

Also it can be found by:

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

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

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507105

This 9223372036854775808LL is a positive number. So you need to take

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

into consideration. Negating it immediately afterwards doesn't make the operand of the - operator itself negative.

Upvotes: 13

Related Questions