File: binary.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (189 lines) | stat: -rw-r--r-- 5,846 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*!
 * \file
 * \brief Binary class definition
 * \author Tony Ottosson
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#ifndef BINARY_H
#define BINARY_H

#include <itpp/base/itassert.h>


namespace itpp {

  /*!
    \brief Binary arithmetic (boolean) class
    \author Tony Ottosson

    This class creates a binary arithmetic class, following the ordinary
    rules for binary (GF(2)) fields.

    Examples:
    \code
    bin a;         // Creation of variable
    bin a = 0;     // Creating a variable and assigning it value 0
    bin b = 1;     // Creating a variable and assigning it value 1
    bin c = a + b; // XOR operation
    c = !a;        // NOT
    c = a * b;     // AND
    c = a / b;     // OR
    \endcode
  */
  class bin {
  public:
    //! Default constructor
    bin(): b(0) {}

    //! Set the binary object equal to \c value. Either "0" or "1".
    bin(const int &value): b(static_cast<char>(value)) {
      it_assert_debug((value == 0) || (value == 1),
                      "bin::bin(): value must be 0 or 1");
    }

    //! Copy constructor
    bin(const bin &inbin): b(inbin.b) {}

    //! Assign a value
    void operator=(const int &value) {
      it_assert_debug((value == 0) || (value == 1),
                      "bin::operator=(): value must be 0 or 1");
      b = static_cast<char>(value);
    }

    //! Assign a value
    void operator=(const bin &inbin) { b = inbin.b; }

    //! OR
    void operator/=(const bin &inbin) { b |= inbin.b; }

    //! OR
    void operator|=(const bin &inbin) { b |= inbin.b; }
    //! OR
    bin operator/(const bin &inbin) const { return bin(b | inbin.b); }
    //! OR
    bin operator|(const bin &inbin) const { return bin(b | inbin.b); }

    //! XOR
    void operator+=(const bin &inbin) { b ^= inbin.b; }
    //! XOR
    void operator^=(const bin &inbin) { b ^= inbin.b; }
    //! XOR
    bin operator+(const bin &inbin) const { return bin(b ^ inbin.b); }
    //! XOR
    bin operator^(const bin &inbin) const { return bin(b ^ inbin.b); }
    //! XOR
    void operator-=(const bin &inbin) { b ^= inbin.b; }
    //! XOR
    bin operator-(const bin &inbin) const { return bin(b ^ inbin.b); }
    //! Dummy definition to be able to use vec<bin>
    bin operator-() const { return bin(b); }

    //! AND
    void operator*=(const bin &inbin) { b &= inbin.b; }
    //! AND
    void operator&=(const bin &inbin) { b &= inbin.b; }
    //! AND
    bin operator*(const bin &inbin) const { return bin(b & inbin.b); }
    //! AND
    bin operator&(const bin &inbin) const { return bin(b & inbin.b); }

    //! NOT
    bin operator!(void) const { return bin(b^1); }
    //! NOT
    bin operator~(void) const { return bin(b^1); }

    //! Check if equal
    bool operator==(const bin &inbin) const { return b == inbin.b; }
    //! Check if equal
    bool operator==(const int &i) const { return b == i; }

    //! Check if not equal
    bool operator!=(const bin &inbin) const { return b != inbin.b; }
    //! Check if not equal
    bool operator!=(const int &i) const { return b != i; }

    //! Less than (interpret the binary values {0,1} as integers)
    bool operator<(const bin &inbin) const  { return b < inbin.b; }
    //! Less than equal (interpret the binary values {0,1} as integers)
    bool operator<=(const bin &inbin) const { return b <= inbin.b; }

    //! Greater than (interpret the binary values {0,1} as integers)
    bool operator>(const bin &inbin) const  { return b > inbin.b; }
    //! Greater than equal (interpret the binary values {0,1} as integers)
    bool operator>=(const bin &inbin) const { return b >= inbin.b; }

    //! Convert \c bin to \c short
    operator short() const  { return static_cast<short>(b); }
    //! Convert \c bin to \c int
    operator int() const    { return static_cast<int>(b); }
    //! Convert \c bin to \c bool
    operator bool() const   { return b != 0; }
    //! Convert \c bin to \c float
    operator float() const  { return static_cast<float>(b); }
    //! Convert \c bin to \c double
    operator double() const { return static_cast<double>(b); }

    //! Output the binary value of the object
    char value() const { return b; }

  private:
    char b;
  };

  /*!
    \relatesalso bin
    \brief Output stream of bin
  */
  std::ostream &operator<<(std::ostream &output, const bin &inbin);

  /*!
    \relatesalso bin
    \brief Input stream of bin
  */
  std::istream &operator>>(std::istream &input, bin &outbin);

  /*!
    \relatesalso bin
    \brief absolute value of bin
  */
  inline bin abs(const bin &inbin) { return inbin; }

} // namespace itpp


namespace std { // added 11/2005, EGL

  /*!
    \relatesalso itpp::bin
    \brief absolute value of bin
  */
  inline int abs(const itpp::bin &inbin) { return inbin; }

} // namespace std

#endif // #ifndef BINARY_H