Why can't I use the long long int type? C ++

I'm trying to

long long int l = 42343254325322343224;

      

but to no avail. Why does it tell me "integer constant is too long". I am using a long long int type that must be more than 19 digits long. Am I doing something wrong here or is there a special secret I don't know about yet?

+3


source to share


3 answers


Take a look at Boost.Multiprecision at Boost.Multiprecision

It defines templates and classes for handling large numbers.



Here's an example from the Boost tutorial:

#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;

int128_t v = 1;

// Do some fixed precision arithmetic:
for(unsigned i = 1; i <= 20; ++i)
   v *= i;

std::cout << v << std::endl; // prints 20!

// Repeat at arbitrary precision:
cpp_int u = 1;
for(unsigned i = 1; i <= 100; ++i)
   u *= i;

std::cout << u << std::endl; // prints 100!

      

+4


source


Because this is more, on my x86_64 system, 2^64

//                                         42343254325322343224
// maximum for 8 byte long long int (2^64) 18446744073709551616
//                    (2^64-1 maximum unsigned representable)
std::cout << sizeof(long long int); // 8

      



you should not confuse the number of digits with the number of bits required to represent a number

+5


source


It seems that the value of an integer literal exceeds the allowed value for type long long int

Try the following program to find the maximum values โ€‹โ€‹of the types long long int

andunsigned long long int

#include <iostream>
#include <limits>

int main() 
{
    std::cout << std::numeric_limits<long long int>::max() << std::endl;
    std::cout << std::numeric_limits<unsigned long long int>::max() << std::endl;

    return 0;
}

      

I got the following results on www.ideone.com

9223372036854775807
18446744073709551615

      

You can compare it with the value you provided

42343254325322343224

      

Note that, in general, there is no need to specify a suffix ll

for an integer decimal literal that is so large that it can only be stored in a type long long int

. The compiler itself will determine the most appropriate type ( int

or long int

or long long int

) for the integral decimal literal.

+3


source







All Articles