C++ Program to Implement Bit Array



Bit array is used to store true/false or yes/no type of values in very less memory. In this article, we will see how to create and use a bit array in C++.

What is Bit Array?

Bit array is a special array where each element can hold only 0 or 1. Instead of using a full byte (8 bits) for each boolean value, a bit array stores multiple values using just one bit each. Meaning, in a normal array of bool values, each value takes at least one byte. But in bit array, we store 8 values in one byte. This helps in saving memory. Let's see how bit array works.

How Bit Array Works?

We use bitwise operators to work with bit arrays. We divide the total size by 8 to get number of bytes needed. Then, for setting, clearing, or checking bits, we use left shift (<<), bitwise AND (&), OR (|), and NOT (~) operators.

Steps to Implement Bit Array in C++

  • Step 1: Create a class called BitArray with private members for storing size and the actual byte array.
  • Step 2: In the constructor, calculate the number of bytes required and resize the internal array accordingly.
  • Step 3: Define a function setBit(index) to turn on a specific bit using bitwise OR and shift operator.
  • Step 4: Define a function clearBit(index) to turn off a bit using bitwise AND and NOT operators.
  • Step 5: Define a function getBit(index) to check whether a bit is set or not using bitwise AND.
  • Step 6: Create a function printBits() to show the complete bit array as 0s and 1s.
  • Step 7: Use the main function to test setting, clearing, and getting bit values.

C++ Program to Implement Bit Array

The code below shows how to create a simple bit array in C++. We will write functions to set, clear, and check bits in the array.

#include <iostream>
#include <vector>
using namespace std;

class BitArray {
    vector<unsigned char> arr;
    int size;

public:
    BitArray(int n) {
        size = n;
        arr.resize((n + 7) / 8, 0); // divide by 8 and round up
    }

    void setBit(int index) {
        if (index >= 0 && index < size)
            arr[index / 8] |= (1 << (index % 8));
    }

    void clearBit(int index) {
        if (index >= 0 && index < size)
            arr[index / 8] &= ~(1 << (index % 8));
    }

    bool getBit(int index) {
        if (index >= 0 && index < size)
            return arr[index / 8] & (1 << (index % 8));
        return false;
    }

    void printBits() {
        for (int i = 0; i < size; i++) {
            cout << getBit(i) << " ";
        }
        cout << endl;
    }
};

int main() {
    BitArray bits(16);

    bits.setBit(0);
    bits.setBit(3);
    bits.setBit(7);
    bits.setBit(15);

    cout << "Bit Array after setting some bits:" << endl;
    bits.printBits();

    bits.clearBit(3);
    cout << "After clearing bit at index 3:" << endl;
    bits.printBits();

    cout << "Bit at index 7 is: " << bits.getBit(7) << endl;

    return 0;
}

The output of the above code will be:

Bit Array after setting some bits:
1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 
After clearing bit at index 3:
1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 
Bit at index 7 is: 1
Updated on: 2025-05-05T18:29:04+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements