-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitarray.cpp
996 lines (872 loc) · 31.7 KB
/
bitarray.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/***************************************************************************
* Arrays of Arbitrary Bit Length
*
* File : bitarray.cpp
* Purpose : Provides object with methods for creation and manipulation of
* arbitrary length arrays of bits.
*
* Bit arrays are implemented as vectors of unsigned chars. Bit
* 0 is the MSB of char 0, and the last bit is the least
* significant (non-spare) bit of the last unsigned char.
*
* Example: array of 20 bits (0 through 19) with 8 bit unsigned
* chars requires 3 unsigned chars (0 through 2) to
* store all the bits.
*
* char 0 1 2
* +--------+--------+--------+
* | | | |
* +--------+--------+--------+
* bit 01234567 8911111111111XXXX
* 012345 6789
*
* Author : Michael Dipperstein
* Date : July 23, 2004
*
****************************************************************************
* HISTORY
*
* $Id: bitarray.cpp,v 1.7 2010/02/04 03:31:43 michael Exp $
* $Log: bitarray.cpp,v $
* Revision 1.7 2010/02/04 03:31:43 michael
* Replaced vector<unsigned char> with an array of unsigned char.
*
* Made updates for GCC 4.4.
*
* Revision 1.5 2007/08/06 05:23:29 michael
* Updated for LGPL Version 3.
*
* All methods that don't modify object have been made
* const to increase functionality of const bit_array_c.
*
* All assignment operators return a reference to the object being assigned a value so that operator chaining will work.
*
* Added >> and << operators.
*
* Revision 1.3 2006/04/30 23:34:07 michael
* Improved performance by incorporating Benjamin Schindler's
* <[email protected]> changes to pass arguments as a reference.
*
* Revision 1.2 2004/08/05 22:16:49 michael
* Add overloads for bitwise operators returning values.
* Add a more natural looking way to set bit values.
*
* Revision 1.1.1.1 2004/08/04 13:28:20 michael
* bit_array_c
*
****************************************************************************
*
* Bitarray: An ANSI C++ class for manipulating arbitrary length bit arrays
* Copyright (C) 2004, 2006-2007, 2010 by
* Michael Dipperstein ([email protected])
*
* This file is part of the bit array library.
*
* The bit array library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* The bit array library 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 Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
/***************************************************************************
* INCLUDED FILES
***************************************************************************/
#include <iostream>
#include <climits>
#include "bitarray.h"
using namespace std;
/***************************************************************************
* MACROS
***************************************************************************/
/* make CHAR_BIT 8 if it's not defined in limits.h */
#ifndef CHAR_BIT
#warning CHAR_BIT not defined. Assuming 8 bits.
#define CHAR_BIT 8
#endif
/* position of bit within character */
#define BIT_CHAR(bit) ((bit) / CHAR_BIT)
/* array index for character containing bit */
#define BIT_IN_CHAR(bit) (1 << (CHAR_BIT - 1 - ((bit) % CHAR_BIT)))
/* number of characters required to contain number of bits */
#define BITS_TO_CHARS(bits) ((((bits) - 1) / CHAR_BIT) + 1)
/* most significant bit in a character */
#define MS_BIT (1 << (CHAR_BIT - 1))
/***************************************************************************
* METHODS
***************************************************************************/
/***************************************************************************
* Method : bit_array_c - constructor
* Description: This is the bit_array_c constructor. It reserves memory
* for the vector storing the array.
* Parameters : numBits - number of bits in the array
* Effects : Allocates vectory for array bits
* Returned : None
***************************************************************************/
bit_array_c::bit_array_c(const int numBits):
m_NumBits(numBits)
{
int numBytes;
numBytes = BITS_TO_CHARS(numBits);
/* allocate space for bit array */
m_Array = new unsigned char[numBytes];
/* set all bits to 0 */
fill_n(m_Array, numBytes, 0);
}
/***************************************************************************
* Method : bit_array_c - constructor
* Description: This is the bit_array_c constructor. It copies the
* for contents of a vector of unsigned char into the
* bitarray.
* Parameters : vect - vector to be copied
* numBits - number of bits in the array
* Effects : Allocates vectory for array bits
* Returned : None
***************************************************************************/
bit_array_c::bit_array_c(unsigned char *array, const int numBits):
m_NumBits(numBits),
m_Array(array)
{
}
/***************************************************************************
* Method : ~bit_array_c - destructor
* Description: This is the bit_array_c destructor. At this point it's
* just a place holder.
* Parameters : None
* Effects : None
* Returned : None
***************************************************************************/
bit_array_c::~bit_array_c(void)
{
delete[] m_Array;
}
/***************************************************************************
* Method : Dump
* Description: This method dumps the conents of a bit array to stdout.
* The format of the dump is a series of bytes represented in
* hexadecimal.
* Parameters : outStream - stream to write to
* Effects : Array contents are dumped to stdout
* Returned : None
***************************************************************************/
void bit_array_c::Dump(std::ostream &outStream)
{
int size;
size = BITS_TO_CHARS(m_NumBits);
outStream.width(2);
outStream.fill('0');
outStream << uppercase << hex << (int)(m_Array[0]); /* first byte */
for (int i = 1; i < size; i++)
{
/* remaining bytes with a leading space */
outStream << " ";
outStream.width(2);
outStream.fill('0');
outStream << (int)(m_Array[i]);
}
outStream << dec;
}
/***************************************************************************
* Method : SetAll
* Description: This method sets every bit in the bit array to 1. This
* method uses UCHAR_MAX to determine what it means to set
* all bits in an unsigned char, so it is crucial that the
* machine implementation of unsigned char utilizes all of
* the bits in the memory allocated for an unsigned char.
* Parameters : None
* Effects : Each of the bits used in the bit array are set to 1.
* Unused (spare) bits are set to 0.
* Returned : None
***************************************************************************/
void bit_array_c::SetAll(void)
{
int bits, size;
unsigned char mask;
size = BITS_TO_CHARS(m_NumBits);
/* set bits in all bytes to 1 */
fill_n(m_Array, size, UCHAR_MAX);
/* zero any spare bits so increment and decrement are consistent */
bits = m_NumBits % CHAR_BIT;
if (bits != 0)
{
mask = UCHAR_MAX << (CHAR_BIT - bits);
m_Array[BIT_CHAR(m_NumBits - 1)] = mask;
}
}
/***************************************************************************
* Method : ClearAll
* Description: This method sets every bit in the bit array to 0.
* Parameters : None
* Effects : Each of the bits in the bit array are set to 0.
* Returned : None
***************************************************************************/
void bit_array_c::ClearAll(void)
{
int size;
size = BITS_TO_CHARS(m_NumBits);
/* set bits in all bytes to 0 */
fill_n(m_Array, size, 0);
}
/***************************************************************************
* Method : SetBit
* Description: This method sets a bit in the bit array to 1.
* Parameters : bit - the number of the bit to set
* Effects : The specified bit will be set to 1.
* Returned : None
***************************************************************************/
void bit_array_c::SetBit(const unsigned int bit)
{
if (m_NumBits <= bit)
{
return; /* bit out of range */
}
m_Array[BIT_CHAR(bit)] |= BIT_IN_CHAR(bit);
}
/***************************************************************************
* Method : ClearBit
* Description: This method sets a bit in the bit array to 0.
* Parameters : bit - the number of the bit to clear
* Effects : The specified bit will be set to 0.
* Returned : None
***************************************************************************/
void bit_array_c::ClearBit(const unsigned int bit)
{
unsigned char mask;
if (m_NumBits <= bit)
{
return; /* bit out of range */
}
/* create a mask to zero out desired bit */
mask = BIT_IN_CHAR(bit);
mask = ~mask;
m_Array[BIT_CHAR(bit)] &= mask;
}
/***************************************************************************
* Method : operator()
* Description: Overload of the () operator. This method approximates
* array indices used for assignment. It returns a
* bit_array_index_c which includes an = method used to
* set bit values.
* Parameters : bit - index of array bit
* Effects : None
* Returned : bit_array_index_c (pointer to bit)
***************************************************************************/
bit_array_index_c bit_array_c::operator()(const unsigned int bit)
{
bit_array_index_c result(this, bit);
return result;
}
/***************************************************************************
* Method : operator[]
* Description: Overload of the [] operator. This method returns the
* value of a bit in the bit array.
* Parameters : bit - index of array bit
* Effects : None
* Returned : The value of the specified bit.
***************************************************************************/
bool bit_array_c::operator[](const unsigned int bit) const
{
return((m_Array[BIT_CHAR(bit)] & BIT_IN_CHAR(bit)) != 0);
}
/***************************************************************************
* Method : operator==
* Description: overload of the == operator
* Parameters : other - bit array to compare
* Effects : None
* Returned : True if this == other. Otherwise false.
***************************************************************************/
bool bit_array_c::operator==(const bit_array_c &other) const
{
if (m_NumBits != other.m_NumBits)
{
/* unequal sizes */
return false;
}
return (this->m_Array == other.m_Array);
}
/***************************************************************************
* Method : operator!=
* Description: overload of the != operator
* Parameters : other - bit array to compare
* Effects : None
* Returned : True if this != other. Otherwise false.
***************************************************************************/
bool bit_array_c::operator!=(const bit_array_c &other) const
{
if (m_NumBits != other.m_NumBits)
{
/* unequal sizes */
return true;
}
return (this->m_Array != other.m_Array);
}
/***************************************************************************
* Method : operator<
* Description: overload of the < operator
* Parameters : other - bit array to compare
* Effects : None
* Returned : True if this < other. Otherwise false.
***************************************************************************/
bool bit_array_c::operator<(const bit_array_c &other) const
{
if (m_NumBits != other.m_NumBits)
{
/* unequal sizes */
return false;
}
return (this->m_Array < other.m_Array);
}
/***************************************************************************
* Method : operator<=
* Description: overload of the <= operator
* Parameters : other - bit array to compare
* Effects : None
* Returned : True if this <= other. Otherwise false.
***************************************************************************/
bool bit_array_c::operator<=(const bit_array_c &other) const
{
if (m_NumBits != other.m_NumBits)
{
/* unequal sizes */
return false;
}
return (this->m_Array <= other.m_Array);
}
/***************************************************************************
* Method : operator>
* Description: overload of the > operator
* Parameters : other - bit array to compare
* Effects : None
* Returned : True if this > other. Otherwise false.
***************************************************************************/
bool bit_array_c::operator>(const bit_array_c &other) const
{
if (m_NumBits != other.m_NumBits)
{
/* unequal sizes */
return false;
}
return (this->m_Array > other.m_Array);
}
/***************************************************************************
* Method : operator>=
* Description: overload of the >= operator
* Parameters : other - bit array to compare
* Effects : None
* Returned : True if this >= other. Otherwise false.
***************************************************************************/
bool bit_array_c::operator>=(const bit_array_c &other) const
{
if (m_NumBits != other.m_NumBits)
{
/* unequal sizes */
return false;
}
return (this->m_Array >= other.m_Array);
}
/***************************************************************************
* Method : operator~
* Description: overload of the ~ operator. Negates all non-spare bits in
* bit array
* Parameters : None
* Effects : None
* Returned : value of this after bitwise not
***************************************************************************/
bit_array_c bit_array_c::operator~(void) const
{
bit_array_c result(this->m_NumBits);
result = *this;
result.Not();
return result;
}
/***************************************************************************
* Method : operator&
* Description: overload of the & operator. Performs a bitwise and
* between the source array and this bit array.
* Parameters : other - bit array on righthand side of &
* Effects : None
* Returned : value of bitwise and of this and other.
***************************************************************************/
bit_array_c bit_array_c::operator&(const bit_array_c &other) const
{
bit_array_c result(this->m_NumBits);
result = *this;
result &= other;
return result;
}
/***************************************************************************
* Method : operator^
* Description: overload of the ^ operator. Performs a bitwise xor
* between the source array and this bit array.
* Parameters : other - bit array on righthand side of ^
* Effects : None
* Returned : value of bitwise xor of this and other.
***************************************************************************/
bit_array_c bit_array_c::operator^(const bit_array_c &other) const
{
bit_array_c result(this->m_NumBits);
result = *this;
result ^= other;
return result;
}
/***************************************************************************
* Method : operator|
* Description: overload of the | operator. Performs a bitwise or
* between the source array and this bit array.
* Parameters : other - bit array on righthand side of |
* Effects : None
* Returned : value of bitwise or of this and other.
***************************************************************************/
bit_array_c bit_array_c::operator|(const bit_array_c &other) const
{
bit_array_c result(this->m_NumBits);
result = *this;
result |= other;
return result;
}
/***************************************************************************
* Method : operator<<
* Description: overload of the << operator. Performs a bitwise left
* shift of this bit array.
* Parameters : count - the number of bits to shift left
* Effects : None
* Returned : result of bitwise left shift
***************************************************************************/
bit_array_c bit_array_c::operator<<(const unsigned int count) const
{
bit_array_c result(this->m_NumBits);
result = *this;
result <<= count;
return result;
}
/***************************************************************************
* Method : operator>>
* Description: overload of the >> operator. Performs a bitwise right
* shift of this bit array.
* Parameters : count - the number of bits to shift right
* Effects : None
* Returned : result of bitwise right shift
***************************************************************************/
bit_array_c bit_array_c::operator>>(const unsigned int count) const
{
bit_array_c result(this->m_NumBits);
result = *this;
result >>= count;
return result;
}
/***************************************************************************
* Method : operator++ (prefix)
* Description: overload of the ++ operator. Increments the contents of
* a bit array. Overflows cause rollover.
* Parameters : None
* Effects : Bit array contents are incremented
* Returned : Reference to this array after increment
***************************************************************************/
bit_array_c& bit_array_c::operator++(void)
{
int i;
unsigned char maxValue; /* maximum value for current char */
unsigned char one; /* least significant bit in current char */
if (m_NumBits == 0)
{
return *this; /* nothing to increment */
}
/* handle arrays that don't use every bit in the last character */
i = (m_NumBits % CHAR_BIT);
if (i != 0)
{
maxValue = UCHAR_MAX << (CHAR_BIT - i);
one = 1 << (CHAR_BIT - i);
}
else
{
maxValue = UCHAR_MAX;
one = 1;
}
for (i = BIT_CHAR(m_NumBits - 1); i >= 0; i--)
{
if (m_Array[i] != maxValue)
{
m_Array[i] = m_Array[i] + one;
return *this;
}
else
{
/* need to carry to next byte */
m_Array[i] = 0;
/* remaining characters must use all bits */
maxValue = UCHAR_MAX;
one = 1;
}
}
return *this;
}
/***************************************************************************
* Method : operator++ (postfix)
* Description: overload of the ++ operator. Increments the contents of
* a bit array. Overflows cause rollover.
* Parameters : dumy - needed for postfix increment
* Effects : Bit array contents are incremented
* Returned : Reference to this array after increment
***************************************************************************/
bit_array_c& bit_array_c::operator++(int dummy)
{
++(*this);
return *this;
}
/***************************************************************************
* Method : operator-- (prefix)
* Description: overload of the -- operator. Decrements the contents of
* a bit array. Underflows cause rollover.
* Parameters : None
* Effects : Bit array contents are decremented
* Returned : None
***************************************************************************/
bit_array_c& bit_array_c::operator--(void)
{
int i;
unsigned char maxValue; /* maximum value for current char */
unsigned char one; /* least significant bit in current char */
if (m_NumBits == 0)
{
return *this; /* nothing to decrement */
}
/* handle arrays that don't use every bit in the last character */
i = (m_NumBits % CHAR_BIT);
if (i != 0)
{
maxValue = UCHAR_MAX << (CHAR_BIT - i);
one = 1 << (CHAR_BIT - i);
}
else
{
maxValue = UCHAR_MAX;
one = 1;
}
for (i = BIT_CHAR(m_NumBits - 1); i >= 0; i--)
{
if (m_Array[i] >= one)
{
m_Array[i] = m_Array[i] - one;
return *this;
}
else
{
/* need to borrow from the next byte */
m_Array[i] = maxValue;
/* remaining characters must use all bits */
maxValue = UCHAR_MAX;
one = 1;
}
}
return *this;
}
/***************************************************************************
* Method : operator-- (postfix)
* Description: overload of the -- operator. Decrements the contents of
* a bit array. Underflows cause rollover.
* Parameters : dumy - needed for postfix decrement
* Effects : Bit array contents are decremented
* Returned : None
***************************************************************************/
bit_array_c& bit_array_c::operator--(int dummy)
{
--(*this);
return *this;
}
/***************************************************************************
* Method : operator=
* Description: overload of the = operator. Copies source contents into
* this bit array.
* Parameters : src - Source bit array
* Effects : Source bit array contents are copied into this array
* Returned : Reference to this array after copy
***************************************************************************/
bit_array_c& bit_array_c::operator=(const bit_array_c &src)
{
if (*this == src)
{
/* don't do anything for a self assignment */
return *this;
}
if (m_NumBits != src.m_NumBits)
{
/* don't do assignment with different array sizes */
return *this;
}
if ((m_NumBits == 0) || (src.m_NumBits == 0))
{
/* don't do assignment with unallocated array */
return *this;
}
/* copy bits from source */
int size;
size = BITS_TO_CHARS(m_NumBits);
copy(src.m_Array, &src.m_Array[size], this->m_Array);
return *this;
}
/***************************************************************************
* Method : operator&=
* Description: overload of the &= operator. Performs a bitwise and
* between the source array and this bit array. This bit
* array will contain the result.
* Parameters : src - Source bit array
* Effects : Results of bitwise and are stored in this array
* Returned : Reference to this array after and
***************************************************************************/
bit_array_c& bit_array_c::operator&=(const bit_array_c &src)
{
int size;
size = BITS_TO_CHARS(m_NumBits);
if (m_NumBits != src.m_NumBits)
{
/* don't do assignment with different array sizes */
return *this;
}
/* AND array one unsigned char at a time */
for(int i = 0; i < size; i++)
{
m_Array[i] = m_Array[i] & src.m_Array[i];
}
return *this;
}
/***************************************************************************
* Method : operator^=
* Description: overload of the ^= operator. Performs a bitwise xor
* between the source array and this bit array. This bit
* array will contain the result.
* Parameters : src - Source bit array
* Effects : Results of bitwise xor are stored in this array
* Returned : Reference to this array after xor
***************************************************************************/
bit_array_c& bit_array_c::operator^=(const bit_array_c &src)
{
int size;
size = BITS_TO_CHARS(m_NumBits);
if (m_NumBits != src.m_NumBits)
{
/* don't do assignment with different array sizes */
return *this;
}
/* XOR array one unsigned char at a time */
for(int i = 0; i < size; i++)
{
m_Array[i] = m_Array[i] ^ src.m_Array[i];
}
return *this;
}
/***************************************************************************
* Method : operator|=
* Description: overload of the |= operator. Performs a bitwise or
* between the source array and this bit array. This bit
* array will contain the result.
* Parameters : src - Source bit array
* Effects : Results of bitwise or are stored in this array
* Returned : Reference to this array after or
***************************************************************************/
bit_array_c& bit_array_c::operator|=(const bit_array_c &src)
{
int size;
size = BITS_TO_CHARS(m_NumBits);
if (m_NumBits != src.m_NumBits)
{
/* don't do assignment with different array sizes */
return *this;
}
/* OR array one unsigned char at a time */
for(int i = 0; i < size; i++)
{
m_Array[i] = m_Array[i] | src.m_Array[i];
}
return *this;
}
/***************************************************************************
* Method : Not
* Description: Negates all non-spare bits in bit array.
* Parameters : None
* Effects : Contents of bit array are negated. Any spare bits are
* left at 0.
* Returned : Reference to this array after not
***************************************************************************/
bit_array_c& bit_array_c::Not(void)
{
int bits;
unsigned char mask;
int size;
size = BITS_TO_CHARS(m_NumBits);
if (m_NumBits == 0)
{
/* don't do not with unallocated array */
return *this;
}
/* NOT array one unsigned char at a time */
for(int i = 0; i < size; i++)
{
m_Array[i] = ~m_Array[i];
}
/* zero any spare bits so increment and decrement are consistent */
bits = m_NumBits % CHAR_BIT;
if (bits != 0)
{
mask = UCHAR_MAX << (CHAR_BIT - bits);
m_Array[BIT_CHAR(m_NumBits - 1)] &= mask;
}
return *this;
}
/***************************************************************************
* Method : operator<<=
* Description: overload of the <<= operator. Performs a left shift on
* this bit array. This bit array will contain the result.
* Parameters : shifts - number of bit positions to shift
* Effects : Results of the shifts are stored in this array
* Returned : Reference to this array after shift
***************************************************************************/
bit_array_c& bit_array_c::operator<<=(const unsigned int shifts)
{
int i;
int chars = shifts / CHAR_BIT; /* number of whole byte shifts */
if (shifts >= m_NumBits)
{
/* all bits have been shifted off */
this->ClearAll();
return *this;
}
/* first handle big jumps of bytes */
if (chars > 0)
{
int size;
size = BITS_TO_CHARS(m_NumBits);
for (i = 0; (i + chars) < size; i++)
{
m_Array[i] = m_Array[i + chars];
}
/* now zero out new bytes on the right */
for (i = size; chars > 0; chars--)
{
m_Array[i - chars] = 0;
}
}
/* now we have at most CHAR_BIT - 1 bit shifts across the whole array */
for (i = 0; i < (int)(shifts % CHAR_BIT); i++)
{
for (unsigned int j = 0; j < BIT_CHAR(m_NumBits - 1); j++)
{
m_Array[j] <<= 1;
/* handle shifts across byte bounds */
if (m_Array[j + 1] & MS_BIT)
{
m_Array[j] |= 0x01;
}
}
m_Array[BIT_CHAR(m_NumBits - 1)] <<= 1;
}
return *this;
}
/***************************************************************************
* Method : operator>>=
* Description: overload of the >>= operator. Performs a right shift on
* this bit array. This bit array will contain the result.
* Parameters : shifts - number of bit positions to shift
* Effects : Results of the shifts are stored in this array
* Returned : Reference to this array after shift
***************************************************************************/
bit_array_c& bit_array_c::operator>>=(const unsigned int shifts)
{
int i;
char mask;
int chars = shifts / CHAR_BIT; /* number of whole byte shifts */
if (shifts >= m_NumBits)
{
/* all bits have been shifted off */
this->ClearAll();
return *this;
}
/* first handle big jumps of bytes */
if (chars > 0)
{
for (i = BIT_CHAR(m_NumBits - 1); (i - chars) >= 0; i--)
{
m_Array[i] = m_Array[i - chars];
}
/* now zero out new bytes on the right */
for (; chars > 0; chars--)
{
m_Array[chars - 1] = 0;
}
}
/* now we have at most CHAR_BIT - 1 bit shifts across the whole array */
for (i = 0; i < (int)(shifts % CHAR_BIT); i++)
{
for (unsigned int j = BIT_CHAR(m_NumBits - 1); j > 0; j--)
{
m_Array[j] >>= 1;
/* handle shifts across byte bounds */
if (m_Array[j - 1] & 0x01)
{
m_Array[j] |= MS_BIT;
}
}
m_Array[0] >>= 1;
}
/***********************************************************************
* zero any spare bits that are shifted beyond the end of the bit array
* so that increment and decrement are consistent.
***********************************************************************/
i = m_NumBits % CHAR_BIT;
if (i != 0)
{
mask = UCHAR_MAX << (CHAR_BIT - i);
m_Array[BIT_CHAR(m_NumBits - 1)] &= mask;
}
return *this;
}
/***************************************************************************
* Method : bit_array_index_c - constructor
* Description: This is the bit_array_index_c constructor. It stores a
* pointer to the bit array and the bit index.
* Parameters : array - pointer to bit array
* index - index of bit in array
* Effects : Pointer to bit array and bit index are stored.
* Returned : None
***************************************************************************/
bit_array_index_c::bit_array_index_c(bit_array_c *array,
const unsigned int index)
{
m_BitArray = array;
m_Index = index;
}
/***************************************************************************
* Method : operator=
* Description: overload of the = operator. Sets the bit array bit to
* the value of src.
* Parameters : src - bit value
* Effects : Bit pointed to by this object is set to the value of
* source.
* Returned : None
***************************************************************************/
void bit_array_index_c::operator=(const bool src)
{
if (m_BitArray == NULL)
{
return; /* no array */
}
if (m_BitArray->Size() <= m_Index)
{
return; /* index is out of bounds */
}
if (src)
{
m_BitArray->SetBit(m_Index);
}
else
{
m_BitArray->ClearBit(m_Index);
}
}