-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetbit.cpp
More file actions
executable file
·84 lines (63 loc) · 1.3 KB
/
getbit.cpp
File metadata and controls
executable file
·84 lines (63 loc) · 1.3 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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class getbit {
public:
getbit() {};
int run(int argc, char *argv[]);
bool isint(string&);
bool str2int(string&, int&);
private:
bool hexdec;
};
// Determines if a bit is set in a mask.
// getbit bit mask
bool getbit::isint(string& s)
{
bool isnumber = true;
// cout << "hexdec: " << (hexdec ? "true" : "false") << endl;
for(string::const_iterator k = s.begin(); k != s.end(); ++k) {
if (hexdec)
isnumber = isnumber && isxdigit(*k);
else
isnumber = isnumber && isdigit(*k);
}
return isnumber;
}
// This code converts from string to number safely.
//
bool getbit::str2int(string& str, int& num)
{
stringstream ss(str);
if (!isint(str))
return false;
if (hexdec)
return (ss >> hex >> num);
else
return (ss >> num);
}
int getbit::run(int argc, char *argv[])
{
int mask;
int bit;
string str;
if(argc < 3)
return 0;
this->hexdec = false;
str = argv[1];
if (!str2int(str, bit))
return 0;
this->hexdec = true;
str = argv[2];
if (!str2int(str, mask))
return 0;
bit = 1 << bit;
// cout << (bit & mask);
return !!(bit & mask);
}
int main(int argc, char *argv[])
{
getbit gb;
return gb.run(argc, argv);
}