Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbitarray.c
More file actions
911 lines (801 loc) · 30 KB
/
Copy pathbitarray.c
File metadata and controls
911 lines (801 loc) · 30 KB
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
/***************************************************************************
* Arrays of Arbitrary Bit Length
*
* File : bitarray.c
* Purpose : Provides functions for creation and manipulation of arbitrary
* length arrays of bits.
*
* Bit arrays are implemented as arrays 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 : January 30, 2004
*
****************************************************************************
*
* Bitarray: An ANSI C library for manipulating arbitrary length bit arrays
* Copyright (C) 2004, 2006-2007, 2014 by
* Michael Dipperstein (mdipperstein@gmail.com)
*
* 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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include "bitarray.h"
/***************************************************************************
* 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))
/***************************************************************************
* TYPE DEFINITIONS
***************************************************************************/
struct bit_array_t
{
unsigned char *array; /* pointer to array containing bits */
unsigned int numBits; /* number of bits in array */
};
/***************************************************************************
* FUNCTIONS
***************************************************************************/
/***************************************************************************
* Function : BitArrayCreate
* Description: This function allocates a bit array large enough to
* contain the specified number of bits. The contents of the
* array are not initialized.
* Parameters : bits - the number of bits in the array to be allocated.
* Effects : allocates bit array from heap, or sets errno on failure.
* Returned : pointer to allocated bit array or NULL if array may not
* be allocated. errno will be set in the event that the
* array may not be allocated.
***************************************************************************/
bit_array_t *BitArrayCreate(const unsigned int bits)
{
bit_array_t *ba;
if (0 == bits)
{
errno = EDOM;
return NULL;
}
/* allocate structure */
ba = (bit_array_t *)malloc(sizeof(bit_array_t));
if (ba == NULL)
{
/* malloc failed */
errno = ENOMEM;
}
else
{
ba->numBits = bits;
/* allocate array */
ba->array = (unsigned char *)malloc(sizeof(unsigned char) *
BITS_TO_CHARS(bits));
if (ba->array == NULL)
{
/* malloc failed */
errno = ENOMEM;
free(ba);
ba = NULL;
}
}
return ba;
}
/***************************************************************************
* Function : BitArrayDestroy
* Description: This function frees the memory allocated for a bit array.
* Parameters : ba - pointer to bit array to be freed
* Effects : frees memory pointed to by bit array structure.
* Returned : None
***************************************************************************/
void BitArrayDestroy(bit_array_t *ba)
{
if (ba != NULL)
{
if (ba->array != NULL)
{
free(ba->array);
}
free(ba);
}
}
/***************************************************************************
* Function : BitArrayDump
* Description: This function dumps the contents of a bit array to the
* specified output stream. The format of the dump is a
* series of bytes represented in hexadecimal.
* Parameters : ba - pointer to bit array to be dumped
* outFile - pointer to output steam to be used. If NULL
* stdout will be used.
* Effects : Hexadecimal dump of array to outFile.
* Returned : None
* NOTE: This function only works with 8 bit characters.
***************************************************************************/
void BitArrayDump(const bit_array_t *const ba, FILE *outFile)
{
unsigned i;
if ((ba == NULL) || (ba->numBits == 0))
{
return; /* nothing to print */
}
if (outFile == NULL)
{
outFile = stdout;
}
fprintf(outFile, "%02X", ba->array[0]); /* first byte */
for (i = 1; i < BITS_TO_CHARS(ba->numBits); i++)
{
/* remaining bytes with a leading space */
fprintf(outFile, " %02X", ba->array[i]);
}
}
/***************************************************************************
* Function : BitArraySetAll
* Description: This function sets every bit to 1 in the bit array passed
* as a parameter. This is function uses UCHAR_MAX, so it is
* crucial that the machine implementation of unsigned char
* utilizes all the bits in the memory allocated for an
* unsigned char.
* Parameters : ba - pointer to bit array
* Effects : Each of the bits used in the bit array are set to 1.
* Unused (spare) bits are set to 0.
* Returned : NONE
***************************************************************************/
void BitArraySetAll(const bit_array_t *const ba)
{
unsigned bits;
unsigned char mask;
if (ba == NULL)
{
return; /* nothing to set */
}
/* set bits in all bytes to 1 */
memset((void *)(ba->array), UCHAR_MAX, BITS_TO_CHARS(ba->numBits));
/* zero any spare bits so increment and decrement are consistent */
bits = ba->numBits % CHAR_BIT;
if (bits != 0)
{
mask = UCHAR_MAX << (CHAR_BIT - bits);
ba->array[BIT_CHAR(ba->numBits - 1)] = mask;
}
}
/***************************************************************************
* Function : BitArrayClearAll
* Description: This function sets every bit to 0 in the bit array passed
* as a parameter.
* Parameters : ba - pointer to bit array
* Effects : Each of the bits used in the bit array are set to 0.
* Returned : NONE
***************************************************************************/
void BitArrayClearAll(const bit_array_t *const ba)
{
if (ba == NULL)
{
return; /* nothing to clear */
}
/* set bits in all bytes to 0 */
memset((void *)(ba->array), 0, BITS_TO_CHARS(ba->numBits));
}
/***************************************************************************
* Function : BitArraySetBit
* Description: This function sets the specified bit to 1 in the bit array
* passed as a parameter.
* Parameters : ba - pointer to bit array
* bit - bit to set
* Effects : The specified bit in the bit array is set to 1.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArraySetBit(const bit_array_t *const ba, const unsigned int bit)
{
if (ba == NULL)
{
return 0; /* no bit to set */
}
if (ba->numBits <= bit)
{
errno = ERANGE;
return -1; /* bit out of range */
}
ba->array[BIT_CHAR(bit)] |= BIT_IN_CHAR(bit);
return 0;
}
/***************************************************************************
* Function : BitArrayClearBit
* Description: This function sets the specified bit to 0 in the bit array
* passed as a parameter.
* Parameters : ba - pointer to bit array
* bit - bit to clear
* Effects : The specified bit in the bit array is set to 0.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayClearBit(const bit_array_t *const ba, const unsigned int bit)
{
unsigned char mask;
if (ba == NULL)
{
return 0; /* no bit to set */
}
if (ba->numBits <= bit)
{
errno = ERANGE;
return -1; /* bit out of range */
}
/* create a mask to zero out desired bit */
mask = BIT_IN_CHAR(bit);
mask = ~mask;
ba->array[BIT_CHAR(bit)] &= mask;
return 0;
}
/***************************************************************************
* Function : BitArrayGetBits
* Description: This function returns a pointer to the array of unsigned
* char containing actual bits. This function should be used
* with caution. Manipulating the array of bits outside of
* the bit array function may have adverse effects.
* Parameters : ba - pointer to bit array
* Effects : None
* Returned : Pointer to array containing bits
***************************************************************************/
void *BitArrayGetBits(const bit_array_t *const ba)
{
if (NULL == ba)
{
return NULL;
}
return ((void *)(ba->array));
}
/***************************************************************************
* Function : BitArrayTestBit
* Description: This function tests the specified bit in the bit array
* passed as a parameter. A non-zero will be returned if the
* tested bit is set.
* Parameters : ba - pointer to bit array
* bit - bit to test
* Effects : None
* Returned : Non-zero if bit is set, otherwise 0. This function does
* not check the input. Tests on invalid input will produce
* unknown results.
***************************************************************************/
int BitArrayTestBit(const bit_array_t *const ba, const unsigned int bit)
{
return((ba->array[BIT_CHAR(bit)] & BIT_IN_CHAR(bit)) != 0);
}
/***************************************************************************
* Function : BitArrayCopy
* Description: This function copies the contents of a source bit array
* into the destination. If the two arrays are not the same
* size or either array pointer is NULL, a copy will not take
* place and errno will be set to EPERM.
* Parameters : dest - pointer to destination bit array
* src - pointer to source bit array
* Effects : The contents of the source bit array are copied to the
* destination bit array.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayCopy(const bit_array_t *const dest, const bit_array_t *const src)
{
if ((NULL == src) || (NULL == dest))
{
errno = EPERM;
return -1; /* no source array */
}
if (src->numBits != dest->numBits)
{
errno = EPERM;
return -1; /* source and destination are not the same size */
}
/* copy source to destination */
memcpy((void *)(dest->array), (void *)(src->array),
BITS_TO_CHARS(src->numBits));
return 0;
}
/***************************************************************************
* Function : BitArrayDuplicate
* Description: This function duplicates (creates and copies) the bit array
* passed as a parameter and returns a pointer to the
* duplicate.
* Parameters : src - pointer to bit array to be duplicated
* Effects : A duplicate of the source bit array is created.
* Returned : Pointer to duplicate of source or NULL on failure. errno
* will be set in the event that the array may not be
* duplicated.
***************************************************************************/
bit_array_t *BitArrayDuplicate(const bit_array_t *const src)
{
bit_array_t *ba;
if (src == NULL)
{
errno = EPERM;
return NULL; /* no source array */
}
ba = BitArrayCreate(src->numBits);
if (ba != NULL)
{
ba->numBits = src->numBits;
BitArrayCopy(ba, src);
}
return ba;
}
/***************************************************************************
* Function : ValidateArgs
* Description: This function validates the arguments passed in to the
* logical operation functions. All pointers must be non-NULL
* and have the same array length.
* Parameters : dest - pointer to destination bit array
* src1 - pointer to first source bit array
* src2 - pointer to second source bit array
* Effects : errno is set when arguments are invalid
* Returned : 0 for success, -1 for failure. errno will be set to EPERM
* in the event of a failure.
***************************************************************************/
int ValidateArgs(const bit_array_t *const dest,
const bit_array_t *const src1,
const bit_array_t *const src2)
{
if ((NULL == src1) || (NULL == src2) || (NULL == dest))
{
errno = EPERM;
return -1; /* NULL source(s) and/or destination */
}
if ((src1->numBits != dest->numBits) || (src2->numBits != dest->numBits))
{
errno = EPERM;
return -1; /* source(s) and/or size mismatch */
}
return 0;
}
/***************************************************************************
* Function : BitArrayAnd
* Description: This function performs a bitwise AND between two bit
* arrays, storing the results in a third bit array. If the
* arrays are NULL or different sizes, no operation will
* will occur.
* Parameters : dest - pointer to destination bit array
* src1 - pointer to first source bit array
* src2 - pointer to second source bit array
* Effects : dest will contain the results of a bitwise AND of src1 and
* src2 unless one array pointer is NULL or arrays are
* different sizes.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayAnd(const bit_array_t *const dest,
const bit_array_t *const src1,
const bit_array_t *const src2)
{
unsigned i;
if (0 != ValidateArgs(dest, src1, src2))
{
return -1;
}
/* AND array one unsigned char at a time */
for(i = 0; i < BITS_TO_CHARS(dest->numBits); i++)
{
dest->array[i] = src1->array[i] & src2->array[i];
}
return 0;
}
/***************************************************************************
* Function : BitArrayOr
* Description: This function performs a bitwise OR between two bit
* arrays, storing the results in a third bit array. If the
* arrays are NULL or different sizes, no operation will
* will occur.
* Parameters : dest - pointer to destination bit array
* src1 - pointer to first source bit array
* src2 - pointer to second source bit array
* Effects : dest will contain the results of a bitwise OR of src1 and
* src2 unless one array pointer is NULL or arrays are
* different sizes.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayOr(const bit_array_t *const dest,
const bit_array_t *const src1,
const bit_array_t *const src2)
{
unsigned i;
if (0 != ValidateArgs(dest, src1, src2))
{
return -1;
}
/* OR array one unsigned char at a time */
for(i = 0; i < BITS_TO_CHARS(dest->numBits); i++)
{
dest->array[i] = src1->array[i] | src2->array[i];
}
return 0;
}
/***************************************************************************
* Function : BitArrayXor
* Description: This function performs a bitwise XOR between two bit
* arrays, storing the results in a third bit array. If the
* arrays are NULL or different sizes, no operation will
* will occur.
* Parameters : dest - pointer to destination bit array
* src1 - pointer to first source bit array
* src2 - pointer to second source bit array
* Effects : dest will contain the results of a bitwise XOR of src1 and
* src2 unless one array pointer is NULL or arrays are
* different sizes.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayXor(const bit_array_t *const dest,
const bit_array_t *const src1,
const bit_array_t *const src2)
{
unsigned i;
if (0 != ValidateArgs(dest, src1, src2))
{
return -1;
}
/* XOR array one unsigned char at a time */
for(i = 0; i < BITS_TO_CHARS(dest->numBits); i++)
{
dest->array[i] = src1->array[i] ^ src2->array[i];
}
return 0;
}
/***************************************************************************
* Function : BitArrayNot
* Description: This function performs a bitwise NOT of one bit array
* storing the results in another bit array. If the arrays
* are NULL or different sizes, no operation will will occur.
* Parameters : dest - pointer to destination bit array
* src - pointer to source bit array
* Effects : dest will contain the results of a bitwise NOT of src
* unless one array pointer is NULL or arrays are different
* sizes.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayNot(const bit_array_t *const dest,
const bit_array_t *const src)
{
unsigned i;
unsigned bits;
unsigned char mask;
if ((NULL == src) || (NULL == dest))
{
errno = EPERM;
return -1; /* NULL source and/or destination */
}
if (src->numBits != dest->numBits)
{
errno = EPERM;
return -1; /* size mismatch */
}
/* NOT array one unsigned char at a time */
for(i = 0; i < BITS_TO_CHARS(dest->numBits); i++)
{
dest->array[i] = ~(src->array[i]);
}
/* zero any spare bits so increment and decrement are consistent */
bits = dest->numBits % CHAR_BIT;
if (bits != 0)
{
mask = UCHAR_MAX << (CHAR_BIT - bits);
dest->array[BIT_CHAR(dest->numBits - 1)] &= mask;
}
return 0;
}
/***************************************************************************
* Function : BitArrayShiftLeft
* Description: This function shifts the bits in a bit array to the left
* by the amount of positions specified.
* Parameters : ba - pointer to the bit array 0 is the msb of the first
* unsigned char in the bit array.
* shifts - number of bits to shift by.
* Effects : The bit array data pointed to by ba is shifted to the left.
* New bits shifted in will be set to 0.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayShiftLeft(const bit_array_t *const ba, unsigned int shifts)
{
unsigned i;
unsigned j;
unsigned chars;
if (NULL == ba)
{
errno = EPERM;
return -1; /* not permitted on NULL array */
}
chars = shifts / CHAR_BIT; /* number of whole byte shifts */
shifts = shifts % CHAR_BIT; /* number of bit shifts remaining */
if (shifts >= ba->numBits)
{
/* all bits have been shifted off */
BitArrayClearAll(ba);
return 0;
}
/* first handle big jumps of bytes */
if (chars != 0)
{
for (i = 0; (i + chars) < BITS_TO_CHARS(ba->numBits); i++)
{
ba->array[i] = ba->array[i + chars];
}
/* now zero out new bytes on the right */
for (i = BITS_TO_CHARS(ba->numBits); chars != 0; chars--)
{
ba->array[i - chars] = 0;
}
}
/* now we have at most CHAR_BIT - 1 bit shifts across the whole array */
for (i = 0; i < shifts; i++)
{
for (j = 0; j < BIT_CHAR(ba->numBits - 1); j++)
{
ba->array[j] <<= 1;
/* handle shifts across byte bounds */
if (ba->array[j + 1] & MS_BIT)
{
ba->array[j] |= 0x01;
}
}
ba->array[BIT_CHAR(ba->numBits - 1)] <<= 1;
}
return 0;
}
/***************************************************************************
* Function : BitArrayShiftRight
* Description: This function shifts the bits in a bit array to the right
* by the amount of positions specified.
* Parameters : ba - pointer to the bit array 0 is the msb of the first
* unsigned char in the bit array.
* shifts - number of bits to shift by.
* Effects : The bit array data pointed to by ba is shifted to the
* right. New bits shifted in will be set to 0.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayShiftRight(const bit_array_t *const ba, unsigned int shifts)
{
unsigned i;
unsigned j;
unsigned char mask;
unsigned chars;
if (NULL == ba)
{
errno = EPERM;
return -1; /* not permitted on NULL array */
}
chars = shifts / CHAR_BIT; /* number of whole byte shifts */
shifts = shifts % CHAR_BIT; /* number of bit shifts remaining */
if (shifts >= ba->numBits)
{
/* all bits have been shifted off */
BitArrayClearAll(ba);
return 0;
}
/* first handle big jumps of bytes */
if (chars > 0)
{
for (i = BIT_CHAR(ba->numBits - 1); i >= chars; i--)
{
ba->array[i] = ba->array[i - chars];
}
/* now zero out new bytes on the right */
for (; chars > 0; chars--)
{
ba->array[chars - 1] = 0;
}
}
/* now we have at most CHAR_BIT - 1 bit shifts across the whole array */
for (i = 0; i < shifts; i++)
{
for (j = BIT_CHAR(ba->numBits - 1); j > 0; j--)
{
ba->array[j] >>= 1;
/* handle shifts across byte bounds */
if (ba->array[j - 1] & 0x01)
{
ba->array[j] |= MS_BIT;
}
}
ba->array[0] >>= 1;
}
/***********************************************************************
* zero any spare bits that are beyond the end of the bit array so
* increment and decrement are consistent.
***********************************************************************/
i = ba->numBits % CHAR_BIT;
if (i != 0)
{
mask = UCHAR_MAX << (CHAR_BIT - i);
ba->array[BIT_CHAR(ba->numBits - 1)] &= mask;
}
return 0;
}
/***************************************************************************
* Function : BitArrayIncrement
* Description: This function increments a bit array as if it is an
* unsigned value of the specified number of bits.
* Parameters : ba - pointer bit array to be incremented
* Effects : ba will contain the results of an increment operation
* performed on itself. Any spare bits in the array of
* unsigned characters containing the bits will be set to 0.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayIncrement(const bit_array_t *const ba)
{
int i;
unsigned char maxValue; /* maximum value for current char */
unsigned char one; /* least significant bit in current char */
if (NULL == ba)
{
errno = EPERM;
return -1; /* not permitted on NULL array */
}
/* handle arrays that don't use every bit in the last character */
i = (ba->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(ba->numBits - 1); i >= 0; i--)
{
if (ba->array[i] != maxValue)
{
ba->array[i] = ba->array[i] + one;
return 0;
}
else
{
/* need to carry to next byte */
ba->array[i] = 0;
/* remaining characters must use all bits */
maxValue = UCHAR_MAX;
one = 1;
}
}
return 0;
}
/***************************************************************************
* Function : BitArrayDecrement
* Description: This function decrements a bit array as if it is an
* unsigned value of the specified number of bits.
* Parameters : ba - pointer bit array to be decremented
* Effects : ba will contain the results of a decrement operation
* performed on itself. Any spare bits in the array of
* unsigned characters containing the bits will be set to 0.
* Returned : 0 for success, -1 for failure. errno will be set in the
* event of a failure.
***************************************************************************/
int BitArrayDecrement(const bit_array_t *const ba)
{
int i;
unsigned char maxValue; /* maximum value for current char */
unsigned char one; /* least significant bit in current char */
if (NULL == ba)
{
errno = EPERM;
return -1; /* not permitted on NULL array */
}
/* handle arrays that don't use every bit in the last character */
i = (ba->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(ba->numBits - 1); i >= 0; i--)
{
if (ba->array[i] >= one)
{
ba->array[i] = ba->array[i] - one;
return 0;
}
else
{
/* need to borrow from the next byte */
ba->array[i] = maxValue;
/* remaining characters must use all bits */
maxValue = UCHAR_MAX;
one = 1;
}
}
return 0;
}
/***************************************************************************
* Function : BitArrayCompare
* Description: This function compares two bit arrays.
* Parameters : ba1 - pointer to bit array
* ba2 - pointer to bit array
* Effects : None
* Returned : < 0 if ba1 < ba2 or ba1 is shorter than ba2
* 0 if ba1 == ba2
* > 0 if ba1 > ba2 or ba1 is longer than ba2
***************************************************************************/
int BitArrayCompare(const bit_array_t *ba1, const bit_array_t *ba2)
{
unsigned i;
if (ba1 == NULL)
{
if (ba2 == NULL)
{
return 0; /* both are NULL */
}
else
{
return -(ba2->numBits); /* ba2 is the only Non-NULL*/
}
}
if (ba2 == NULL)
{
return (ba1->numBits); /* ba1 is the only Non-NULL*/
}
if (ba1->numBits != ba2->numBits)
{
/* arrays are different sizes */
return (ba1->numBits - ba2->numBits);
}
for(i = 0; i <= BIT_CHAR(ba1->numBits - 1); i++)
{
if (ba1->array[i] != ba2->array[i])
{
/* found a non-matching unsigned char */
return(ba1->array[i] - ba2->array[i]);
}
}
return 0;
}
You can’t perform that action at this time.
