1 | /*! |
---|
2 | * \file |
---|
3 | * \brief Binary class definition |
---|
4 | * \author Tony Ottosson |
---|
5 | * |
---|
6 | * ------------------------------------------------------------------------- |
---|
7 | * |
---|
8 | * IT++ - C++ library of mathematical, signal processing, speech processing, |
---|
9 | * and communications classes and functions |
---|
10 | * |
---|
11 | * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) |
---|
12 | * |
---|
13 | * This program is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * This program is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with this program; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
26 | * |
---|
27 | * ------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | |
---|
30 | #ifndef BINARY_H |
---|
31 | #define BINARY_H |
---|
32 | |
---|
33 | #include <itpp/base/itassert.h> |
---|
34 | |
---|
35 | |
---|
36 | namespace itpp { |
---|
37 | |
---|
38 | /*! |
---|
39 | \brief Binary arithmetic (boolean) class |
---|
40 | \author Tony Ottosson |
---|
41 | |
---|
42 | This class creates a binary aritmetic class, following the ordinary |
---|
43 | rules for binary (GF(2)) fields. |
---|
44 | |
---|
45 | Examples: |
---|
46 | \code |
---|
47 | bin a; // Creation of variable |
---|
48 | bin a = 0; // Creating a variable and assigning it value 0 |
---|
49 | bin b = 1; // Creating a variable and assigning it value 1 |
---|
50 | bin c = a + b; // XOR operation |
---|
51 | c = !a; // NOT |
---|
52 | c = a * b; // AND |
---|
53 | c = a / b; // OR |
---|
54 | \endcode |
---|
55 | */ |
---|
56 | class bin { |
---|
57 | public: |
---|
58 | //! Default constructor |
---|
59 | bin(): b(0) {} |
---|
60 | |
---|
61 | //! Set the binary object equal to \c value. Either "0" or "1". |
---|
62 | bin(const int &value): b(static_cast<char>(value)) { |
---|
63 | it_assert_debug((value == 0) || (value == 1), |
---|
64 | "bin::bin(): value must be 0 or 1"); |
---|
65 | } |
---|
66 | |
---|
67 | //! Copy constructor |
---|
68 | bin(const bin &inbin): b(inbin.b) {} |
---|
69 | |
---|
70 | //! Assign a value |
---|
71 | void operator=(const int &value) { |
---|
72 | it_assert_debug((value == 0) || (value == 1), |
---|
73 | "bin::operator=(): value must be 0 or 1"); |
---|
74 | b = static_cast<char>(value); |
---|
75 | } |
---|
76 | |
---|
77 | //! Assign a value |
---|
78 | void operator=(const bin &inbin) { b = inbin.b; } |
---|
79 | |
---|
80 | //! OR |
---|
81 | void operator/=(const bin &inbin) { b |= inbin.b; } |
---|
82 | |
---|
83 | //! OR |
---|
84 | void operator|=(const bin &inbin) { b |= inbin.b; } |
---|
85 | //! OR |
---|
86 | bin operator/(const bin &inbin) const { return bin(b | inbin.b); } |
---|
87 | //! OR |
---|
88 | bin operator|(const bin &inbin) const { return bin(b | inbin.b); } |
---|
89 | |
---|
90 | //! XOR |
---|
91 | void operator+=(const bin &inbin) { b ^= inbin.b; } |
---|
92 | //! XOR |
---|
93 | void operator^=(const bin &inbin) { b ^= inbin.b; } |
---|
94 | //! XOR |
---|
95 | bin operator+(const bin &inbin) const { return bin(b ^ inbin.b); } |
---|
96 | //! XOR |
---|
97 | bin operator^(const bin &inbin) const { return bin(b ^ inbin.b); } |
---|
98 | //! XOR |
---|
99 | void operator-=(const bin &inbin) { b ^= inbin.b; } |
---|
100 | //! XOR |
---|
101 | bin operator-(const bin &inbin) const { return bin(b ^ inbin.b); } |
---|
102 | //! Dummy definition to be able to use vec<bin> |
---|
103 | bin operator-() const { return bin(b); } |
---|
104 | |
---|
105 | //! AND |
---|
106 | void operator*=(const bin &inbin) { b &= inbin.b; } |
---|
107 | //! AND |
---|
108 | void operator&=(const bin &inbin) { b &= inbin.b; } |
---|
109 | //! AND |
---|
110 | bin operator*(const bin &inbin) const { return bin(b & inbin.b); } |
---|
111 | //! AND |
---|
112 | bin operator&(const bin &inbin) const { return bin(b & inbin.b); } |
---|
113 | |
---|
114 | //! NOT |
---|
115 | bin operator!(void) const { return bin(b^1); } |
---|
116 | //! NOT |
---|
117 | bin operator~(void) const { return bin(b^1); } |
---|
118 | |
---|
119 | //! Check if equal |
---|
120 | bool operator==(const bin &inbin) const { return b == inbin.b; } |
---|
121 | //! Check if equal |
---|
122 | bool operator==(const int &i) const { return b == i; } |
---|
123 | |
---|
124 | //! Check if not equal |
---|
125 | bool operator!=(const bin &inbin) const { return b != inbin.b; } |
---|
126 | //! Check if not equal |
---|
127 | bool operator!=(const int &i) const { return b != i; } |
---|
128 | |
---|
129 | //! Less than (interpret the binary values {0,1} as integers) |
---|
130 | bool operator<(const bin &inbin) const { return b < inbin.b; } |
---|
131 | //! Less than equal (interpret the binary values {0,1} as integers) |
---|
132 | bool operator<=(const bin &inbin) const { return b <= inbin.b; } |
---|
133 | |
---|
134 | //! Greater than (interpret the binary values {0,1} as integers) |
---|
135 | bool operator>(const bin &inbin) const { return b > inbin.b; } |
---|
136 | //! Greater than equal (interpret the binary values {0,1} as integers) |
---|
137 | bool operator>=(const bin &inbin) const { return b >= inbin.b; } |
---|
138 | |
---|
139 | //! Convert \c bin to \c short |
---|
140 | operator short() const { return static_cast<short>(b); } |
---|
141 | //! Convert \c bin to \c int |
---|
142 | operator int() const { return static_cast<int>(b); } |
---|
143 | //! Convert \c bin to \c bool |
---|
144 | operator bool() const { return b != 0; } |
---|
145 | //! Convert \c bin to \c float |
---|
146 | operator float() const { return static_cast<float>(b); } |
---|
147 | //! Convert \c bin to \c double |
---|
148 | operator double() const { return static_cast<double>(b); } |
---|
149 | |
---|
150 | //! Output the binary value of the object |
---|
151 | char value() const { return b; } |
---|
152 | |
---|
153 | private: |
---|
154 | char b; |
---|
155 | }; |
---|
156 | |
---|
157 | /*! |
---|
158 | \relatesalso bin |
---|
159 | \brief Output stream of bin |
---|
160 | */ |
---|
161 | std::ostream &operator<<(std::ostream &output, const bin &inbin); |
---|
162 | |
---|
163 | /*! |
---|
164 | \relatesalso bin |
---|
165 | \brief Input stream of bin |
---|
166 | */ |
---|
167 | std::istream &operator>>(std::istream &input, bin &outbin); |
---|
168 | |
---|
169 | /*! |
---|
170 | \relatesalso bin |
---|
171 | \brief absolute value of bin |
---|
172 | */ |
---|
173 | inline bin abs(const bin &inbin) { return inbin; } |
---|
174 | |
---|
175 | } // namespace itpp |
---|
176 | |
---|
177 | |
---|
178 | namespace std { // added 11/2005, EGL |
---|
179 | |
---|
180 | /*! |
---|
181 | \relatesalso itpp::bin |
---|
182 | \brief absolute value of bin |
---|
183 | */ |
---|
184 | inline int abs(const itpp::bin &inbin) { return inbin; } |
---|
185 | |
---|
186 | } // namespace std |
---|
187 | |
---|
188 | #endif // #ifndef BINARY_H |
---|