, , , , , ,

Having trouble getting integers larger than 64 bits to work in C++? So was I. Here’s how I fixed it:

  1. You’ll want to use the data type cpp_int provided by Boost C++ Libraries.
    1. Go here and download the zip: http://sourceforge.net/projects/boost/files/boost/1.56.0/
    2. Unzip into a forever home. (You’ll probably want this again sometime, so don’t just stick it in your project folder and forget about it.)
  2. Code::BlocksCode::Blocks needs some priming before you can use Boost:
    1. With your project open, choose: Project->Build Options….
    2. Select the Search Directories tab.
    3. Use the Add button at the bottom to add the path to your new Boost directory. This needs to be the directory that was created when you extracted the zip file. (In my case, I added /Users/jmatthewturner/C++/Boost/boost_1_56_0.)
  3. And your project file needs some special code:
    1. Add an #include for the cpp_int.hpp file that is inside boost/multiprecision/. Mine looks like this:
      #include "/Users/jmatthewturner/C++/Boost/boost_1_56_0/boost/multiprecision/cpp_int.hpp"

Now you can use the data type cpp_int, which is an arbitrarily large integer, limited only by the memory available on your machine. It is part of the namespace boost::multiprecision, so always refer to it like this:

boost::multiprecision::cpp_int myBigInteger;

or define a namespace to make your life a little easier:

using namespace std;
namespace mp = boost::multiprecision;
mp::cpp_int myBigInteger;
mp::cpp_int myFunction(mp::cpp_int x);

…etc, etc. And that should do it. I used cpp_int to help solve Project Euler #20; I’ll post the source code when I get around to writing up the problem.