Repository navigation
Expand file tree
/
Copy pathexample3.cpp
More file actions
83 lines (72 loc) · 2.47 KB
/
Copy pathexample3.cpp
File metadata and controls
83 lines (72 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2001 Jeremy Siek
// Copyright (c) 2008 Gennaro Prota
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Reads a bitset from the file named on the command line or, if no file
// name is given, from the standard input.
//
// Sample run:
//
// mask = 101010101010
// x.size() = 0
// Enter a bitset in binary: x = 100100010
//
// Input number: 100100010
// x.size() is now: 9
// As unsigned long: 290
// Mask (possibly resized): 010101010
// And with mask: 000100010
// Or with mask: 110101010
// Shifted left by 1: 001000100
// Shifted right by 1: 010010001
#include "boost/dynamic_bitset.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdexcept>
int
main( int argc, char * argv[] )
{
std::ifstream file;
if ( argc > 1 ) {
file.open( argv[ 1 ] );
}
std::istream & in = argc > 1 ? file : std::cin;
boost::dynamic_bitset<> mask( 12, 2730ul );
std::cout << "mask = " << mask << std::endl;
boost::dynamic_bitset<> x;
std::cout << "x.size() = " << x.size() << std::endl;
std::cout << "Enter a bitset in binary: x = " << std::flush;
if ( in >> x ) {
const std::size_t sz = x.size();
std::cout << std::endl;
std::cout << "Input number: " << x << std::endl;
std::cout << "x.size() is now: " << sz << std::endl;
bool fits_in_ulong = true;
unsigned long ul = 0;
try {
ul = x.to_ulong();
} catch ( std::overflow_error & ) {
fits_in_ulong = false;
}
std::cout << "As unsigned long: ";
if ( fits_in_ulong ) {
std::cout << ul;
} else {
std::cout << "(overflow exception)";
}
std::cout << std::endl;
mask.resize( sz );
std::cout << "Mask (possibly resized): " << mask << std::endl;
std::cout << "And with mask: " << ( x & mask ) << std::endl;
std::cout << "Or with mask: " << ( x | mask ) << std::endl;
std::cout << "Shifted left by 1: " << ( x << 1 ) << std::endl;
std::cout << "Shifted right by 1: " << ( x >> 1 ) << std::endl;
} else {
std::cerr << "Couldn't read a bitset" << std::endl;
return EXIT_FAILURE;
}
return 0;
}