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 
bin operator!(void) const
NOT.
Definition: binary.h:116
void operator &=(const bin &inbin)
AND.
Definition: binary.h:109
bin(const bin &inbin)
Copy constructor.
Definition: binary.h:69
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
bin operator*(const bin &inbin) const
AND.
Definition: binary.h:111
STL namespace.
bool operator!=(const int &i) const
Check if not equal.
Definition: binary.h:128
bool operator<(const bin &inbin) const
Less than (interpret the binary values {0,1} as integers)
Definition: binary.h:131
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
void operator^=(const bin &inbin)
XOR.
Definition: binary.h:94
void operator|=(const bin &inbin)
OR.
Definition: binary.h:85
void operator=(const int &value)
Assign a value.
Definition: binary.h:72
void operator+=(const bin &inbin)
XOR.
Definition: binary.h:92
bool operator==(const int &i) const
Check if equal.
Definition: binary.h:123
bin operator-(const bin &inbin) const
XOR.
Definition: binary.h:102
void operator-=(const bin &inbin)
XOR.
Definition: binary.h:100
bool operator<=(const bin &inbin) const
Less than equal (interpret the binary values {0,1} as integers)
Definition: binary.h:133
bin operator-() const
Dummy definition to be able to use vec<bin>
Definition: binary.h:104
void operator/=(const bin &inbin)
OR.
Definition: binary.h:82
itpp namespace
Definition: itmex.h:36
bin operator &(const bin &inbin) const
AND.
Definition: binary.h:113
bool operator!=(const bin &inbin) const
Check if not equal.
Definition: binary.h:126
Error handling functions - header file.
bool operator==(const bin &inbin) const
Check if equal.
Definition: binary.h:121
void operator=(const bin &inbin)
Assign a value.
Definition: binary.h:79
bin()
Default constructor.
Definition: binary.h:60
bin(const int &value)
Set the binary object equal to value. Either "0" or "1".
Definition: binary.h:63
char value() const
Output the binary value of the object.
Definition: binary.h:152
bin operator~(void) const
NOT.
Definition: binary.h:118
void operator*=(const bin &inbin)
AND.
Definition: binary.h:107
bin operator+(const bin &inbin) const
XOR.
Definition: binary.h:96
bool operator>=(const bin &inbin) const
Greater than equal (interpret the binary values {0,1} as integers)
Definition: binary.h:138
Binary arithmetic (boolean) class.
Definition: binary.h:56
bin operator|(const bin &inbin) const
OR.
Definition: binary.h:89
bin abs(const bin &inbin)
absolute value of bin
Definition: binary.h:174
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition: binary.cpp:42
bool operator>(const bin &inbin) const
Greater than (interpret the binary values {0,1} as integers)
Definition: binary.h:136
bin operator/(const bin &inbin) const
OR.
Definition: binary.h:87
bin operator^(const bin &inbin) const
XOR.
Definition: binary.h:98
SourceForge Logo

Generated on Sun Apr 10 2022 12:00:00 for IT++ by Doxygen 1.8.14