cpp_int_import_export.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2015 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. #include <boost/multiprecision/cpp_int.hpp>
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <vector>
  9. #include <iterator>
  10. //[IE1
  11. /*`
  12. In this simple example, we'll import/export the bits of a cpp_int
  13. to a vector of 8-bit unsigned values:
  14. */
  15. /*=
  16. #include <boost/multiprecision/cpp_int.hpp>
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <vector>
  20. #include <iterator>
  21. */
  22. int main()
  23. {
  24. using boost::multiprecision::cpp_int;
  25. // Create a cpp_int with just a couple of bits set:
  26. cpp_int i;
  27. bit_set(i, 5000); // set the 5000'th bit
  28. bit_set(i, 200);
  29. bit_set(i, 50);
  30. // export into 8-bit unsigned values, most significant bit first:
  31. std::vector<unsigned char> v;
  32. export_bits(i, std::back_inserter(v), 8);
  33. // import back again, and check for equality:
  34. cpp_int j;
  35. import_bits(j, v.begin(), v.end());
  36. BOOST_ASSERT(i == j);
  37. }
  38. //]