This repository has been archived by the owner on May 29, 2024. It is now read-only.
Adding Bit Array Data Structure with all the basic operations #1323
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Checklist:
What kind of change does this PR introduce? (check at least one)
Briefly describe the changes in this PR
BitArray Structure
BitArray
struct consists of anunsigned int* array
pointer to store the actual bits and asize_t size
to keep track of the number of bits.unsigned int
:unsigned int
is used for the storage array to ensure that the storage is treated as a sequence of bits without a sign bit. This choice maximizes portability and efficiency for bitwise operations.Key Functions and Operations
Creating and Freeing Bit Arrays:
createBitArray(size_t n)
: Allocates memory for a new bit array ofn
bits, initializing all bits to 0.freeBitArray(BitArrayPtr bit_array)
: Frees the allocated memory for a bit array to prevent memory leaks.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.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).Utility and Convenience:
printBitArray
: Prints the bit array for debugging and visualization.bitwiseShiftLeft
,bitwiseShiftRight
,bitwiseRotateLeft
, andbitwiseRotateRight
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: