31 int Bitset::Length() {
34 ostream &Bitset::Output(ostream &os)
const {
35 deque<bool>::const_iterator iter=bits.begin();
36 while (iter != bits.end()) {
43 void Bitset::clear() { bits.clear(); }
44 void Bitset::push_back(
const bool bit) { bits.push_back(bit); }
45 void Bitset::push_back(
const unsigned char b,
int bit_count ) {
46 push_back((
const unsigned long)b, bit_count);
48 void Bitset::push_back(
const unsigned short s,
int bit_count ) {
49 push_back((
const unsigned long)s, bit_count);
51 void Bitset::push_back(
const unsigned long l,
int bit_count ) {
53 if ((bit_count > 32) || (bit_count == 0)) bit_count=32;
54 mask = 0x01<<(bit_count-1);
55 for (
int i=0; i<bit_count; i++) {
56 if (l & mask) push_back(
true);
57 else push_back(
false);
61 void Bitset::push_back_meaningful(
const unsigned long l) {
63 for (
int i=0; i<32; i++) {
64 unsigned long mask = 0x01<<i;
65 if (l & mask) bit_count = i+1;
67 push_back(l, bit_count);
69 void Bitset::fill_zeros_left(
size_t bit_count) {
70 while (bits.size() < bit_count) {
71 bits.push_front(
false);
75 void Bitset::push_back(
string s) {
76 string::const_iterator iter = s.begin();
77 while (iter != s.end()) {
78 unsigned char c = *iter;
83 bool Bitset::pop_front()
85 bool ret = bits.front();
89 bool Bitset::pop_back()
91 bool ret = bits.back();
96 void Bitset::flip(
size_t pos) {
97 bits[pos] = !bits[pos];
103 ss.unsetf(std::ios_base::dec);
104 ss.setf(std::ios_base::hex);
106 int bitpos = (0x08 << (bits.size() % 4));
107 if (bitpos > 0x08) bitpos >>= 4;
108 for (
size_t i=0; i < bits.size(); i++) {
109 if (bits[i]) b = b | bitpos;
110 else b = b & (0x0f ^ bitpos);
112 if (bitpos == 0x00) {
120 unsigned long Bitset::ulong()
125 ss << setbase(16) << hex();
131 unsigned char Bitset::uchar()
136 ss << setbase(16) << hex();
139 return (
unsigned char)v;
142 void BitsetExt::hamming_enc_block(
unsigned long block_len, deque<bool>::iterator &iter) {
143 if (verbose) cout<<
"hamming_enc_block: ";
144 unsigned long next_parity=1;
145 for (
unsigned long i=1; i<=block_len; i++) {
147 if (i == next_parity) {
148 if (verbose) cout<<
"p";
150 iter = bits.insert(iter,
false);
154 if (iter == bits.end()) {
158 if (verbose) cout<<(*iter?1:0);
160 unsigned long parity = next_parity>>1;
163 deque<bool>::iterator parity_iter=(iter - (i - parity));
164 *parity_iter = !*parity_iter;
174 if (block_len == (next_parity >> 1)) {
176 for (
unsigned long ii=1; ii<block_len; ii++) {
177 if (*(iter-ii-1)) *(iter-1) = !*(iter-1);
182 for (
unsigned long ii=block_len; ii>=1; ii--) {
183 cout<<(*(iter-ii)?1:0);
185 cout<<
" block_len: "<<block_len<<endl;
188 int BitsetExt::hamming_dec_block(
unsigned long block_len, deque<bool>::iterator &iter) {
189 if (verbose) cout<<
"hamming_dec_block: ";
190 bool potentially_double_error =
false;
191 unsigned long total_parity=0;
192 unsigned long parity=0;
193 unsigned long next_parity=1;
194 for (
unsigned long i=1; i<=block_len; i++) {
195 if (iter == bits.end()) {
207 total_parity = total_parity ^ 1;
209 if (i == next_parity) {
210 if (verbose) cout<<
"("<<*iter<<
")";
212 iter = bits.erase(iter);
214 if (verbose) cout<<*iter;
219 if (verbose) cout<<
" too short"<<endl;
222 if (block_len == (next_parity >> 1)) {
223 parity = parity & ~(next_parity >> 1);
224 if (total_parity == 0) {
225 potentially_double_error =
true;
229 if (verbose) cout<<
" parity: "<<parity;
231 if (potentially_double_error) {
232 if (verbose) cout<<
" double error"<<endl;
236 for (
unsigned long i=1; i<=block_len; i++) {
237 if (i == next_parity) {
240 if (verbose) cout<<
" parity bit error"<<endl;
243 }
else if (i >= parity) {
247 iter[-steps] = !iter[-steps];
248 if (verbose) cout<<
" corrected"<<endl;
251 if (verbose) cout<<
" ok"<<endl;
254 BitsetExt::BitsetExt() {
257 BitsetExt::BitsetExt(
bool _verbose) {
258 SetVerbose(_verbose);
275 void BitsetExt::SetVerbose(
bool _verbose) {
278 int BitsetExt::count_hamming_enc_len(
int block_len,
int dec_len) {
280 int dec_len_count = dec_len;
281 while (dec_len_count > 0) {
282 unsigned long next_parity = 1;
283 for (
unsigned long i=1; i<=(
unsigned long)block_len; i++) {
284 if (i == next_parity) {
290 if (dec_len_count == 0)
break;
293 return dec_len + parity_len;
295 int BitsetExt::count_hamming_dec_len(
int block_len,
int enc_len) {
297 int enc_len_count = enc_len;
298 while (enc_len_count > 0) {
299 unsigned long next_parity = 1;
301 for (i=1; i<=(
unsigned long)block_len; i++) {
302 if (i == next_parity) {
307 if (enc_len_count == 0)
break;
310 return enc_len - parity_len;
312 void BitsetExt::hamming_enc(
int block_len) {
313 deque<bool>::iterator iter=bits.begin();
314 while (iter != bits.end()) {
315 hamming_enc_block(block_len, iter);
319 int BitsetExt::hamming_dec(
int block_len) {
321 deque<bool>::iterator iter=bits.begin();
322 while (iter != bits.end()) {
323 int error=hamming_dec_block(block_len, iter);
324 if ((error == -1) || (error_count == -1)) error_count=-1;
325 else error_count += error;
This file implements bit set handling.