### Array of bits

• Bit arrays

• Fact:

 The C programming language does not provide support for array of bits

However:

 The C programming language provides all the necessary operations to allow the programmer (= you) to implement an "array of bits" data structure.

• Implementing an array of bits

• What is an array of bits:

• An array of bits A is an array where:

 A[i] = 0 or 1

• Trivial implementation:

• Use an array of char

 You waste 7/8 (87.5%) of the memory space !!!

• Efficient implementation:

• Use an array of int (because the most cost efficient access to memory is accessing int sized data):

• Map the bit array onto the array of integer as follows:

• Mapping function:

• Element with index k in the bit array is:

 bit position k % 32       in the array element with index k/32

• Bit manipulation functions

• SetBit(A, k): set the kth bit in the bit array A:

 ``` void SetBit( int A[ ], int k ) { int i = k/32; int pos = k%32; unsigned int flag = 1; // flag = 0000.....00001 flag = flag << pos; // flag = 0000...010...000 (shifted k positions) A[i] = A[i] | flag; // Set the bit at the k-th position in A[i] } ```

Shortened version:

 ``` void SetBit( int A[ ], int k ) { A[k/32] |= 1 << (k%32); // Set the bit at the k-th position in A[i] } ```

• ClearBit(A, k): clear the kth bit in the bit array A:

 ``` void ClearBit( int A[ ], int k ) { int i = k/32; int pos = k%32; unsigned int flag = 1; // flag = 0000.....00001 flag = flag << pos; // flag = 0000...010...000 (shifted k positions) flag = ~flag; // flag = 1111...101..111 A[i] = A[i] & flag; // RESET the bit at the k-th position in A[i] } ```

Shortened version:

 ``` void ClearBit( int A[ ], int k ) { A[k/32] &= ~(1 << (k%32)); } ```

• TestBit(A, k): test the kth bit in the bit array A:

 ``` int TestBit( int A[ ], int k ) { int i = k/32; int pos = k%32; unsigned int flag = 1; // flag = 0000.....00001 flag = flag << pos; // flag = 0000...010...000 (shifted k positions) if ( A[i] & flag ) // Test the bit at the k-th position in A[i] return 1; else return 0; } ```

Shortened version:

 ``` int TestBit( int A[ ], int k ) { return ( (A[k/32] & (1 << (k%32) )) != 0 ) ; } ```

• Example Program: (Demo above code)

• Example C program that uses the bit array:

 ``` #include #include "bit-manipulation.h" int main( int argc, char* argv[] ) { int A[10]; // This is our bit array (32×10 = 320 bits !) int i; for ( i = 0; i < 10; i++ ) A[i] = 0; // Clear the bit array printf("Set bit poistions 100, 200 and 300\n"); SetBit( A, 100 ); // Set 3 bits SetBit( A, 200 ); SetBit( A, 300 ); // Check if SetBit() works: for ( i = 0; i < 320; i++ ) if ( TestBit(A, i) ) printf("Bit %d was set !\n", i); printf("\nClear bit poistions 200 \n"); ClearBit( A, 200 ); // Check if ClearBit() works: for ( i = 0; i < 320; i++ ) if ( TestBit(A, i) ) printf("Bit %d was set !\n", i); } ```

• Example Program: (Demo above code)

How to run the program:

• Right click on link(s) and save in a scratch directory

• To compile the normal version:

 ``` gcc -c bit-manipulation.c gcc -c bit-array1.c gcc bit-array1.o bit-manipulation.o ```

• To compile the shortened version:

 ``` gcc -c bit-manipulation1.c gcc -c bit-array1.c gcc bit-array1.o bit-manipulation1.o ```

• To run:          ./a.out

• Bit manipulations as macros

• Bit manipulations are usually written as macros:

 ``` #define SetBit(A,k) ( A[(k/32)] |= (1 << (k%32)) ) #define ClearBit(A,k) ( A[(k/32)] &= ~(1 << (k%32)) ) #define TestBit(A,k) ( A[(k/32)] & (1 << (k%32)) ) ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc bit-array2.c To run:          a.out