Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main.exe
81 changes: 50 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

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;
}
~~~
54 changes: 54 additions & 0 deletions src/Bitmask.c
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions src/Bitmask.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef BITMASK_H
#define BITMASK_H

#include <stdint.h>
#include <stdlib.h>

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 */
62 changes: 0 additions & 62 deletions src/bitmap.c

This file was deleted.

21 changes: 0 additions & 21 deletions src/bitmap.h

This file was deleted.

49 changes: 29 additions & 20 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
#include "Bitmask.h"
#include <stdio.h>
#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;
}