• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2001 Jeremy Siek
2 // Copyright (c) 2008 Gennaro Prota
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Sample run:
8 //
9 // mask     = 101010101010
10 // x.size() = 0
11 // Enter a bitset in binary: x = 100100010
12 //
13 // Input number:             100100010
14 // x.size() is now:          9
15 // As unsigned long:         290
16 // Mask (possibly resized):  010101010
17 // And with mask:            000100010
18 // Or with mask:             110101010
19 // Shifted left by 1:        001000100
20 // Shifted right by 1:       010010001
21 
22 
23 
24 #include "boost/dynamic_bitset.hpp"
25 
26 #include <ostream>
27 #include <iostream>
28 
main()29 int main()
30 {
31     boost::dynamic_bitset<> mask(12, 2730ul);
32     std::cout << "mask     = " << mask << std::endl;
33 
34     boost::dynamic_bitset<> x;
35     std::cout << "x.size() = " << x.size() << std::endl;
36 
37     std::cout << "Enter a bitset in binary: x = " << std::flush;
38     if (std::cin >> x) {
39         const std::size_t sz = x.size();
40         std::cout << std::endl;
41         std::cout << "Input number:             " << x << std::endl;
42         std::cout << "x.size() is now:          " << sz << std::endl;
43 
44         bool fits_in_ulong = true;
45         unsigned long ul = 0;
46         try {
47             ul = x.to_ulong();
48         } catch(std::overflow_error &) {
49             fits_in_ulong = false;
50         }
51 
52         std::cout << "As unsigned long:         ";
53         if(fits_in_ulong) {
54             std::cout << ul;
55         } else {
56             std::cout << "(overflow exception)";
57         }
58 
59         std::cout << std::endl;
60 
61         mask.resize(sz);
62 
63         std::cout << "Mask (possibly resized):  " << mask << std::endl;
64 
65         std::cout << "And with mask:            " << (x & mask) << std::endl;
66         std::cout << "Or with mask:             " << (x | mask) << std::endl;
67         std::cout << "Shifted left by 1:        " << (x << 1) << std::endl;
68         std::cout << "Shifted right by 1:       " << (x >> 1) << std::endl;
69     }
70     return 0;
71 }
72