2

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, (i.e. 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
Bush
  • 2,433
  • 5
  • 34
  • 57

2 Answers2

2

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 limbs are grouped.

sehe
  • 374,641
  • 47
  • 450
  • 633
0

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

marom
  • 5,064
  • 10
  • 14
  • This is not a bad idea, although it has two issues: the initializer assumes the compiler has 128bit literal support. Second, "you cannot have directly what you ask" is a bit of a stretch. I haven't checked how much of the backend API is documented, but parts of it certainly are. – sehe Mar 16 '16 at 14:55