IT++ Logo
binary.h
Go to the documentation of this file.
1 
29 #ifndef BINARY_H
30 #define BINARY_H
31 
32 #include <itpp/base/itassert.h>
33 #include <itpp/itexports.h>
34 
35 namespace itpp
36 {
37 
56 class bin
57 {
58 public:
60  bin(): b(0) {}
61 
63  bin(const int &value): b(static_cast<char>(value)) {
64  it_assert_debug((value == 0) || (value == 1),
65  "bin::bin(): value must be 0 or 1");
66  }
67 
69  bin(const bin &inbin): b(inbin.b) {}
70 
72  void operator=(const int &value) {
73  it_assert_debug((value == 0) || (value == 1),
74  "bin::operator=(): value must be 0 or 1");
75  b = static_cast<char>(value);
76  }
77 
79  void operator=(const bin &inbin) { b = inbin.b; }
80 
82  void operator/=(const bin &inbin) { b |= inbin.b; }
83 
85  void operator|=(const bin &inbin) { b |= inbin.b; }
87  bin operator/(const bin &inbin) const { return bin(b | inbin.b); }
89  bin operator|(const bin &inbin) const { return bin(b | inbin.b); }
90 
92  void operator+=(const bin &inbin) { b ^= inbin.b; }
94  void operator^=(const bin &inbin) { b ^= inbin.b; }
96  bin operator+(const bin &inbin) const { return bin(b ^ inbin.b); }
98  bin operator^(const bin &inbin) const { return bin(b ^ inbin.b); }
100  void operator-=(const bin &inbin) { b ^= inbin.b; }
102  bin operator-(const bin &inbin) const { return bin(b ^ inbin.b); }
104  bin operator-() const { return bin(b); }
105 
107  void operator*=(const bin &inbin) { b &= inbin.b; }
109  void operator&=(const bin &inbin) { b &= inbin.b; }
111  bin operator*(const bin &inbin) const { return bin(b & inbin.b); }
113  bin operator&(const bin &inbin) const { return bin(b & inbin.b); }
114 
116  bin operator!(void) const { return bin(b ^ 1); }
118  bin operator~(void) const { return bin(b ^ 1); }
119 
121  bool operator==(const bin &inbin) const { return b == inbin.b; }
123  bool operator==(const int &i) const { return b == i; }
124 
126  bool operator!=(const bin &inbin) const { return b != inbin.b; }
128  bool operator!=(const int &i) const { return b != i; }
129 
131  bool operator<(const bin &inbin) const { return b < inbin.b; }
133  bool operator<=(const bin &inbin) const { return b <= inbin.b; }
134 
136  bool operator>(const bin &inbin) const { return b > inbin.b; }
138  bool operator>=(const bin &inbin) const { return b >= inbin.b; }
139 
141  operator short() const { return static_cast<short>(b); }
143  operator int() const { return static_cast<int>(b); }
145  operator bool() const { return b != 0; }
147  operator float() const { return static_cast<float>(b); }
149  operator double() const { return static_cast<double>(b); }
150 
152  char value() const { return b; }
153 
154 private:
155  char b;
156 };
157 
162 ITPP_EXPORT std::ostream &operator<<(std::ostream &output, const bin &inbin);
163 
168 ITPP_EXPORT std::istream &operator>>(std::istream &input, bin &outbin);
169 
174 inline bin abs(const bin &inbin) { return inbin; }
175 
176 } // namespace itpp
177 
178 
179 namespace std // added 11/2005, EGL
180 {
181 
186 inline int abs(const itpp::bin &inbin) { return inbin; }
187 
188 } // namespace std
189 
190 #endif // #ifndef BINARY_H
191 
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:19 for IT++ by Doxygen 1.8.2