简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM