MakeContributions / DSA

Data Structure and Algorithm (DSA) contributions
MIT License
713 stars 769 forks source link

Adding Bit Array Data Structure with all the basic operations #1323

Open AnuragUmale opened 6 months ago

AnuragUmale commented 6 months ago

PR Checklist:

What kind of change does this PR introduce? (check at least one)

Briefly describe the changes in this PR

BitArray Structure

Key Functions and Operations

  1. Creating and Freeing Bit Arrays:

    • createBitArray(size_t n): Allocates memory for a new bit array of n bits, initializing all bits to 0.
    • freeBitArray(BitArrayPtr bit_array): Frees the allocated memory for a bit array to prevent memory leaks.
  2. Basic Bit Manipulations:

    • setBit(BitArray* ba, size_t index): Sets a bit to 1 at the specified index.
    • clearBit(BitArray* ba, size_t index): Clears a bit (sets to 0) at the specified index.
    • toggleBit(BitArray* ba, size_t index): Toggles a bit at the specified index.
    • testBit(const BitArray* ba, size_t index): Tests whether a bit at the specified index is set or not.
  3. Advanced Operations:

    • findFirstSetBit, findFirstUnsetBit: Searches for the first set (1) or unset (0) bit in the array.
    • hammingWeightBitArray: Calculates the Hamming weight (number of set bits).
    • Bitwise operations like AND, OR, NOT, XOR, shifts, and rotates, allowing for complex manipulation of bit sets.
  4. Utility and Convenience:

    • printBitArray: Prints the bit array for debugging and visualization.
    • Operations like bitwiseShiftLeft, bitwiseShiftRight, bitwiseRotateLeft, and bitwiseRotateRight provide ways to shift or rotate bits within the array, which can be useful for algorithms that require bit manipulation, such as cryptography or compression algorithms.
    • bitSlice, unionBitArrays, intersectionBitArrays, differenceBitArrays, resizeBitArray: These functions provide additional operations like slicing a bit array, performing set operations (union, intersection, difference), and resizing a bit array.

Other information:

Use Cases and Applications

Bit arrays are widely used in situations where memory efficiency is crucial, and operations can be represented as sets of binary states. Common applications include:

welcome[bot] commented 6 months ago

Thanks for opening this pull request! Please check out our contributing guidelines.