diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0abd7ea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main.exe \ No newline at end of file diff --git a/README.md b/README.md index 40d8216..eaf7b55 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,76 @@ -# Bitmap Library -This is a C library for handling bitmaps. It includes functions for initializing a BitmapSet, setting and unsetting bits, retrieving the value of a bit, and printing the binary value of a BitmapSet. +# Bitmask Library + +This is a C library for handling bitmasks. It includes functions for initializing a Bitmask, Setting and Unsetting bits and retrieving the value of a bit. ## Getting Started -Include the bitmap.h header file in your project. -~~~C -#include "bitmap.h" -~~~ +1. Copy Bitmask.h and Bitmask.c in your Project Source + +2. Include the Bitmask.h header file in your project. + + ~~~C + #include "Bitmask.h" + ~~~ ## Initialization -Before using the bitmap, you need to initialize it by calling `initializeBitmapSet()` function. + +Before using the bitmask, you need to initialize it by calling `Bitmask_Init(size_t Size)` function. ~~~C -BitmapSet bitmapSet; -initializeBitmapSet(&bitmapSet); +size_t Size = 6; +Bitmask* bitmask; +bitmask = Bitmask_Init(Size); ~~~ ## Basic Operations -- Use `setValue()` to set a bit in the bitmap. -- Use `getValue()` to retrieve the value of a bit in the bitmap. -- Use `getSize()` to retrieve the number of set bits in the bitmap. -- Use `unsetValue()` to unset a bit in the bitmap. -- Use `printBitMap()` to print the binary value of the bitmap. +- Use `Bitmask_Init(size_t Size)` to initialize + a new `Bitmask*`. +- Use `Bitmask_Set(Bitmask* bitmap, size_t Index)` to set (or set to 1) a bit in the bitmask. +- Use `Bitmask_Unset(Bitmask* bitmap, size_t Index)` to unset (or set to 0) a bit in the bitmask. +- Use `Bitmask_Get(Bitmask* bitmap, size_t Index)` to retrieve the value of a bit in the bitmask. Note: If bitmap is null then it will return -1. ## Example -Here is an example of how to use the Bitmap Library: +Here is an example of how to use the Bitmask Library: ~~~c -#include "bitmap.h" +#include "Bitmask.h" #include -int main() { - BitmapSet bitmapSet; - initializeBitmapSet(&bitmapSet); +#define Size 10 +Bitmask* mask; + +void PrintAll() +{ + for (size_t i = 0; i < Size; ++i) + printf("%d", Bitmask_Get(mask, i)); + printf("\n"); +} + +int main() +{ + mask = Bitmask_Init(Size); + + Bitmask_Set(mask, 0); + Bitmask_Set(mask, 2); + Bitmask_Set(mask, 5); + Bitmask_Set(mask, 7); + Bitmask_Set(mask, 6); + + PrintAll(); - setValue(&bitmapSet, 1); - setValue(&bitmapSet, 3); - setValue(&bitmapSet, 5); - setValue(&bitmapSet, 7); + printf("Value at Index 2: %d\n", Bitmask_Get(mask, 2)); + printf("Value at Index 7: %d\n", Bitmask_Get(mask, 7)); - printf("Value at index 5: %u\n", getValue(&bitmapSet, 5)); - printf("Number of set bits: %u\n", getSize(&bitmapSet)); + Bitmask_Unset(mask, 2); - unsetValue(&bitmapSet, 3); + printf("Value at Index 2: %d\n", Bitmask_Get(mask, 2)); - printf("Value at index 3: %u\n", getValue(&bitmapSet, 3)); - printf("Number of set bits: %u\n", getSize(&bitmapSet)); + printf("Size of Bitmask: %d\n", mask->size); - printf("Binary value: "); - printBitMap(&bitmapSet); + PrintAll(); - return 0; + return 0; } ~~~ diff --git a/src/Bitmask.c b/src/Bitmask.c new file mode 100644 index 0000000..5d4ddbe --- /dev/null +++ b/src/Bitmask.c @@ -0,0 +1,54 @@ +#include "Bitmask.h" + +Bitmask *Bitmask_Init(size_t Size) +{ + Bitmask *mask = (Bitmask *)malloc(sizeof(Bitmask)); + + if (mask == NULL) + return NULL; // Failed to allocate memory for the mask + + mask->size = Size; + // Allocate memory for the bits + mask->bits = (uint8_t *)calloc((Size + 7) / 8, sizeof(uint8_t)); + + if (mask->bits == NULL) + { + free(mask); // Clean up allocated memory for the mask + return NULL; // Failed to allocate memory for the bits + } + + return mask; +} + +void Bitmask_Destroy(Bitmask *bitmap) +{ + if (bitmap != NULL) + { + free(bitmap->bits); + free(bitmap); + } +} + +void Bitmask_Set(Bitmask* bitmap, size_t Index) +{ + if (bitmap == NULL || Index >= bitmap->size) + return; + + bitmap->bits[Index / 8] |= (1 << (Index % 8)); +} + +void Bitmask_Unset(Bitmask* bitmap, size_t Index) +{ + if (bitmap == NULL || Index >= bitmap->size) + return; + + bitmap->bits[Index / 8] &= ~(1 << (Index % 8)); +} + +int8_t Bitmask_Get(Bitmask* bitmap, size_t Index) +{ + if (bitmap == NULL || Index >= bitmap->size) + return -1; + + return (bitmap->bits[Index / 8] >> (Index % 8)) & 1; +} diff --git a/src/Bitmask.h b/src/Bitmask.h new file mode 100644 index 0000000..52c4342 --- /dev/null +++ b/src/Bitmask.h @@ -0,0 +1,20 @@ +#ifndef BITMASK_H +#define BITMASK_H + +#include +#include + +typedef struct { + uint8_t *bits; + size_t size; +} Bitmask; + +Bitmask* Bitmask_Init(size_t Size); +void Bitmask_Destroy(Bitmask* bitmap); + +void Bitmask_Set(Bitmask* bitmap, size_t Index); +void Bitmask_Unset(Bitmask* bitmap, size_t Index); + +int8_t Bitmask_Get(Bitmask* bitmap, size_t Index); + +#endif /* BITMASK_H */ \ No newline at end of file diff --git a/src/bitmap.c b/src/bitmap.c deleted file mode 100644 index f3967c4..0000000 --- a/src/bitmap.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "bitmap.h" -#include -#include - -#define BITMAP_SIZE 32 -#define MAX_BIT_INDEX (BITMAP_SIZE - 1) - -void initializeBitmapSet(BitmapSet *bitmapSet) { - bitmapSet->map = 0; - bitmapSet->size = 0; -} - -void setValue(BitmapSet *bitmapSet, uint8_t bitIndex) { - if (bitIndex > MAX_BIT_INDEX) { - return; - } - - if (getValue(bitmapSet, bitIndex)) { - return; - } - - uint32_t mask = 1; - bitmapSet->map |= (mask << (MAX_BIT_INDEX - bitIndex)); - bitmapSet->size++; -} - -uint8_t getValue(const BitmapSet *bitmapSet, uint8_t bitIndex) { - if (bitIndex > MAX_BIT_INDEX) { - return 0; - } - - return (bitmapSet->map & (1 << (MAX_BIT_INDEX - bitIndex))) != 0; -} - -uint8_t getSize(const BitmapSet *bitmapSet) { return bitmapSet->size; } - -void unsetValue(BitmapSet *bitmapSet, uint8_t bitIndex) { - if (bitIndex > MAX_BIT_INDEX) { - return; - } - - if (!getValue(bitmapSet, bitIndex)) { - return; - } - - uint32_t mask = UINT32_MAX ^ (1 << (MAX_BIT_INDEX - bitIndex)); - bitmapSet->map &= mask; - bitmapSet->size--; -} - -void printBitMap(const BitmapSet *bitmapSet) { - uint32_t bitIndex = MAX_BIT_INDEX; - printf("\n"); - while (bitIndex != UINT32_MAX) { - printf("%d", (bitmapSet->map & (1 << bitIndex)) ? 1 : 0); - if (bitIndex % 4 == 0) { - printf(" "); - } - bitIndex--; - } - printf("\n"); -} \ No newline at end of file diff --git a/src/bitmap.h b/src/bitmap.h deleted file mode 100644 index c609f80..0000000 --- a/src/bitmap.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef BITMAPSET_H -#define BITMAPSET_H - -#include - -#define BITMAP_SIZE 32 -#define MAX_BIT_INDEX (BITMAP_SIZE - 1) - -typedef struct { - uint32_t map; - uint8_t size; -} BitmapSet; - -void initializeBitmapSet(BitmapSet *bitmapSet); -void setValue(BitmapSet *bitmapSet, uint8_t bitIndex); -uint8_t getValue(const BitmapSet *bitmapSet, uint8_t bitIndex); -uint8_t getSize(const BitmapSet *bitmapSet); -void unsetValue(BitmapSet *bitmapSet, uint8_t bitIndex); -void printBitMap(const BitmapSet *bitmapSet); - -#endif /* BITMAPSET_H */ \ No newline at end of file diff --git a/src/main.c b/src/main.c index de89287..156ec83 100644 --- a/src/main.c +++ b/src/main.c @@ -1,34 +1,43 @@ +#include "Bitmask.h" #include -#include "bitmap.h" -int main() { - BitmapSet myBitmap; - initializeBitmapSet(&myBitmap); +//Define a Size +#define Size 10 +Bitmask* mask; + +//A Function to print a binary representation +void PrintAll() +{ + for (size_t i = 0; i < Size; ++i) + printf("%d", Bitmask_Get(mask, i)); + printf("\n"); +} + +int main() +{ + mask = Bitmask_Init(Size); // Set some values - setValue(&myBitmap, 1); - setValue(&myBitmap, 3); - setValue(&myBitmap, 5); - setValue(&myBitmap, 31); + Bitmask_Set(mask, 0); + Bitmask_Set(mask, 2); + Bitmask_Set(mask, 5); + Bitmask_Set(mask, 7); + Bitmask_Set(mask, 6); // Print binary representation - printBitMap(&myBitmap); + PrintAll(); - // Check if a value is set - printf("Value at index 3 is set: %d\n", getValue(&myBitmap, 3)); - printf("Value at index 10 is set: %d\n", getValue(&myBitmap, 10)); + printf("Value at Index 2: %d\n", Bitmask_Get(mask, 2)); + printf("Value at Index 7: %d\n", Bitmask_Get(mask, 7)); - // Get size of the bitmap - printf("Size of the bitmap: %d\n", getSize(&myBitmap)); + Bitmask_Unset(mask, 2); - // Unset a value - unsetValue(&myBitmap, 5); + printf("Value at Index 2: %d\n", Bitmask_Get(mask, 2)); - // Print binary representation again - printBitMap(&myBitmap); + printf("Size of Bitmask: %d\n", mask->size); - // Get updated size of the bitmap - printf("Size of the bitmap: %d\n", getSize(&myBitmap)); + // Print binary representation again + PrintAll(); return 0; } \ No newline at end of file