简体   繁体   中英

Extracting bits from boost multiprecision

I'm using uint256_t to make arithmetic operation on big integers; I would like to extract the bits of the numbers in a regular form, (ie not in a floating point form) without any precision since I'm only using integers and not floats.

For example: if my code has:

#include <boost/multiprecision/cpp_int.hpp>    
uint256_t v = 0xffffffffffffffffffffffffffffff61;

Then I would like to have 32 bytes:

61 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

You might be able to use the backend representation directly.

Check the documentation whether this is part of the public API.

Live On Coliru

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>

int main() {
    using namespace boost::multiprecision;
    uint256_t v("0xffffffffffffffffffffffffffffff61");

    std::copy_n(v.backend().limbs(), v.backend().size(), 
        std::ostream_iterator<unsigned>(std::cout << std::hex << std::showbase, " "));
}

Prints

0xffffff61 0xffffffff 

Drop std::showbase to do without 0x . I picked this representation so it's maximally clear how the limb s are grouped.

You cannot have directly what you ask. Internal rapresentation is not necessarily done that way, one type uses even 'decimal' digits.

You can obtain that indirectly

uint256_t v = 0xffffffffffffffffffffffffffffff61;
std::ostringstream ost ;
ost << std::hex << v ; 

now ost.str() is FFFF....FFF61

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