Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / example / cpp_int_import_export.cpp
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
6 #include <boost/multiprecision/cpp_int.hpp>
7 #include <iostream>
8 #include <iomanip>
9 #include <vector>
10 #include <iterator>
11
12 //[IE1
13
14 /*`
15 In this simple example, we'll import/export the bits of a cpp_int
16 to a vector of 8-bit unsigned values:
17 */
18 /*=
19 #include <boost/multiprecision/cpp_int.hpp>
20 #include <iostream>
21 #include <iomanip>
22 #include <vector>
23 #include <iterator>
24 */
25
26 int main()
27 {
28    using boost::multiprecision::cpp_int;
29    // Create a cpp_int with just a couple of bits set:
30    cpp_int i;
31    bit_set(i, 5000); // set the 5000'th bit
32    bit_set(i, 200);
33    bit_set(i, 50);
34    // export into 8-bit unsigned values, most significant bit first:
35    std::vector<unsigned char> v;
36    export_bits(i, std::back_inserter(v), 8);
37    // import back again, and check for equality:
38    cpp_int j;
39    import_bits(j, v.begin(), v.end());
40    BOOST_ASSERT(i == j);
41 }
42
43 //]