user3243499
user3243499

Reputation: 3161

Bitwise operation using c++ boost library

I am new to the c++ boost library. I want to use bitwise operations on cpp_int type. Below code works fine.

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
namespace mp = boost::multiprecision;
using boost::multiprecision::cpp_int;
using namespace std;

int main(){
    cpp_int p = 2;
    cout<<mp::pow(p, 1024)<<endl;

    return 0;
}

However, when I try to take the shift value from the user, I get an "no match for operator <<" error in the line for (p<<c).

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
namespace mp = boost::multiprecision;
using boost::multiprecision::cpp_int;
using namespace std;

int main(){
    cpp_int p = 2, c;
    //cout<<mp::pow(p, 1024)<<endl;
    cin>>c;
    cout << (p<<c) << endl;

    return 0;
}

Upvotes: 0

Views: 1136

Answers (1)

John Zwinck
John Zwinck

Reputation: 249404

Bitwise shift is only implemented in Boost Multiprecision when the right-hand side is a built-in integral type. You can see that here:

http://www.boost.org/doc/libs/1_64_0/boost/multiprecision/number.hpp

So you can use uint64_t in a loop, shifting by up to UINT64_MAX each time, and decrementing c as you go. Presumably you do not need to shift by more than a few bazillion digits.

Upvotes: 3

Related Questions