IT++ Logo

binary.h

Go to the documentation of this file.
00001 
00030 #ifndef BINARY_H
00031 #define BINARY_H
00032 
00033 #include <itpp/base/itassert.h>
00034 
00035 
00036 namespace itpp
00037 {
00038 
00057 class bin
00058 {
00059 public:
00061   bin(): b(0) {}
00062 
00064   bin(const int &value): b(static_cast<char>(value)) {
00065     it_assert_debug((value == 0) || (value == 1),
00066                     "bin::bin(): value must be 0 or 1");
00067   }
00068 
00070   bin(const bin &inbin): b(inbin.b) {}
00071 
00073   void operator=(const int &value) {
00074     it_assert_debug((value == 0) || (value == 1),
00075                     "bin::operator=(): value must be 0 or 1");
00076     b = static_cast<char>(value);
00077   }
00078 
00080   void operator=(const bin &inbin) { b = inbin.b; }
00081 
00083   void operator/=(const bin &inbin) { b |= inbin.b; }
00084 
00086   void operator|=(const bin &inbin) { b |= inbin.b; }
00088   bin operator/(const bin &inbin) const { return bin(b | inbin.b); }
00090   bin operator|(const bin &inbin) const { return bin(b | inbin.b); }
00091 
00093   void operator+=(const bin &inbin) { b ^= inbin.b; }
00095   void operator^=(const bin &inbin) { b ^= inbin.b; }
00097   bin operator+(const bin &inbin) const { return bin(b ^ inbin.b); }
00099   bin operator^(const bin &inbin) const { return bin(b ^ inbin.b); }
00101   void operator-=(const bin &inbin) { b ^= inbin.b; }
00103   bin operator-(const bin &inbin) const { return bin(b ^ inbin.b); }
00105   bin operator-() const { return bin(b); }
00106 
00108   void operator*=(const bin &inbin) { b &= inbin.b; }
00110   void operator&=(const bin &inbin) { b &= inbin.b; }
00112   bin operator*(const bin &inbin) const { return bin(b & inbin.b); }
00114   bin operator&(const bin &inbin) const { return bin(b & inbin.b); }
00115 
00117   bin operator!(void) const { return bin(b ^ 1); }
00119   bin operator~(void) const { return bin(b ^ 1); }
00120 
00122   bool operator==(const bin &inbin) const { return b == inbin.b; }
00124   bool operator==(const int &i) const { return b == i; }
00125 
00127   bool operator!=(const bin &inbin) const { return b != inbin.b; }
00129   bool operator!=(const int &i) const { return b != i; }
00130 
00132   bool operator<(const bin &inbin) const  { return b < inbin.b; }
00134   bool operator<=(const bin &inbin) const { return b <= inbin.b; }
00135 
00137   bool operator>(const bin &inbin) const  { return b > inbin.b; }
00139   bool operator>=(const bin &inbin) const { return b >= inbin.b; }
00140 
00142   operator short() const  { return static_cast<short>(b); }
00144   operator int() const    { return static_cast<int>(b); }
00146   operator bool() const   { return b != 0; }
00148   operator float() const  { return static_cast<float>(b); }
00150   operator double() const { return static_cast<double>(b); }
00151 
00153   char value() const { return b; }
00154 
00155 private:
00156   char b;
00157 };
00158 
00163 std::ostream &operator<<(std::ostream &output, const bin &inbin);
00164 
00169 std::istream &operator>>(std::istream &input, bin &outbin);
00170 
00175 inline bin abs(const bin &inbin) { return inbin; }
00176 
00177 } // namespace itpp
00178 
00179 
00180 namespace std   // added 11/2005, EGL
00181 {
00182 
00187 inline int abs(const itpp::bin &inbin) { return inbin; }
00188 
00189 } // namespace std
00190 
00191 #endif // #ifndef BINARY_H
00192 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Tue Nov 23 08:47:55 2010 for IT++ by Doxygen 1.6.1