The usage of an array of bits

  • C (and Java) provides the programmer with arrays of the basic data types, such as:

      char  a[10];   // Each array element is 1 byte 
      short b[10];   // Each array element is 2 bytes 
      int   c[10];   // Each array element is 4 bytes   

  • The C programming language does not provide array of bits

  • Why use an array of bits ?

      • A bit can store one of the values 0 or 1

      • A bit array is ideal to store a series of boolean variables

  • I.e.:

      • We can use an array of bits as an array of boolean variables !

The bit array data structure and its operations

  • An array of bits A is an array variable where:

      A[k] = 0 or 1
    
      I.e.: Each element can only take on 
                  (1) the value 0 or 
                  (2) the value 1

  • Operations on a bit array A:

     Operation            Effect
     ================================
     SetBit(A, k)        A[k] = 1
     ClrBit(A, k)        A[k] = 0
     IsSet(A, k)         (A[k] != 0)
    

We will next implement the bit array data structure using an array of int variable

How to implement a bit array using an int array

  • Suppose we have an array of int A[ ]:                                                                                                                                    

  • Each array element A[k] is an int typed variable and contains 32 bits

How to implement a bit array using an int array

  • The bits in each array element A[k] is numbered as follows:                                          

  • We will map the bits in the bit array onto the bits in the int

How to implement a bit array using an int array

  • The first 32 elements (= bits) in the bit array is mapped to A[0] as follows:              

  • We wil use A[1] to map the next 32 elements (= bits) of the bit array !

How to implement a bit array using an int array

  • The second 32 elements (= bits) in the bit array is mapped to A[1] as follows:              

  • We wil use A[2] to map the next 32 elements (= bits) of the bit array !

How to implement a bit array using an int array

  • The third 32 elements (= bits) in the bit array is mapped to A[2] as follows:             

  • And so on...

How to implement a bit array using an int array

  • The array elements of the bit array are mapped to the array elements of the int array A a follows:

  • Question:   how to map a bit array index k to (1) an array element A[i] and (2) the bit position within that array element

How to implement a bit array using an int array

  • Suppose we want to map the bit array element with index = 68:
     

  • How to find the array element and the bit position corresponding to this bit array element?

How to implement a bit array using an int array

  • Because an int variable has 32 bits, we divide the bit array index by 32:                                   

  • Compute the quotient and the remainder !

How to implement a bit array using an int array

  • We can see that: (1) the quotient = index of the int array element and (2) the remainder = bit position within the int array element:

  • Mapping:   bit array element with index k ---> A[k/32] and bit position k%32

Implementations of SetBit(A, k)

  • The SetBit(int A[ ], int k) function: (step-by-step)

    void SetBit(int A[ ], int k)
    {
        int i   = k/32; // Map bit array index --> int array index
        int pos = k%32; // Map bit array index --> bit position
        int pattern =  (1 << pos);  // 000..00100..000
    
        A[i] = A[i] | pattern;  // Set bit at position pos to 1
    }

  • You can write SetBit(int A[ ], int k) much shorter:

    void SetBit(int A[ ], int k)
    {
        A[k/32] = A[k/32] | (1<<(k%32));
    }

  • Or as a macro:

     #define  SetBit(A,k)  ( A[k/32] |= (1<<(k%32)) ) 

Implementations of ClrBit(A, k)

  • The ClrBit(int A[ ], int k) function: (step-by-step)

    void ClrBit(int A[ ], int k)
    {
        int i   = k/32;
        int pos = k%32;
        int pattern = ~(1 << pos);  // 111..11011..111
    
        A[i] = A[i] & pattern;  // Clear bit at position pos to 0
    }

  • You can write ClrBit(int A[ ], int k) much shorter:

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

  • Or as a macro:

     #define  ClrBit(A,k)  ( A[k/32] &= ~(1<<(k%32)) ) 

Implementations of IsSet(A, k)

  • The IsSet(int A[ ], int k) function: (step-by-step)

    int IsSet(int A[ ], int k)
    {
        int i   = k/32;
        int pos = k%32;
        int pattern =  (1 << pos);  // 000..00100..000
    
        return (A[i] & pattern) != 0;  // Test bit at position pos
    }

  • You can write IsSet(int A[ ], int k) much shorter:

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

  • Or as a macro:

     #define  IsSet(A,k)  ( ( A[k/32] & (1<<(k%32)) ) != 0 )

DEMO: demo/C/set1/bit-array1.c