
bit_flag.c
#include <stdio.h>
#include <stdlib.h>
#include "bit_flags.h"
typedef struct bit_flags
{
int size;
int capacity;
int *bit;
} Bit_flags;
BIT_FLAGS bit_flags_init_number_of_bits(int number_of_bits)
{
Bit_flags* pBit_flags;
if(number_of_bits > 0) // The given number is positive
{
pBit_flags = (Bit_flags*)malloc(sizeof(Bit_flags));
if(pBit_flags != NULL)
{
pBit_flags->capacity = sizeof(int);
pBit_flags->size = number_of_bits;
pBit_flags->bit = (int*)malloc(sizeof(int));
if(pBit_flags->bit != NULL)
{
*pBit_flags->bit = 0;
}
else
{
printf("Could not set memory for bit value.\n");
pBit_flags = NULL;
}
}
printf("Bit flags object memory print created!\n\n");
}
else // Given number was 0 or negative
{
printf("Number of bits cannot be negative.\n");
pBit_flags = NULL;
}
return pBit_flags;
}
Status bit_flags_set_flag(BIT_FLAGS hBit_flags, int flag_position)
{
Status stat;
int bit_to_set = flag_position;
int* temp;
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags; // Cast the void* object to a Bit_flag* object
while(flag_position >= phBit_flags->capacity) // While the flag position is bigger than what the object can handle
{
phBit_flags->capacity *= 2; // Double its capacity
temp = (int*)malloc(sizeof(int) * phBit_flags->capacity); // Get new dynamic array with new capacity
if(temp != NULL) // Check if malloc worked
{
*temp = *(phBit_flags->bit);
free(phBit_flags->bit); // Free the dynamic arrays that is too small to hold the requested bit
phBit_flags->bit = temp;
}
else // Malloc failed
{
stat = FAILURE;
printf("Could not resize bit flags object.\n");
return stat;
}
}
flag_position /= phBit_flags->capacity;
phBit_flags->bit[flag_position] |= 1 << (bit_to_set%phBit_flags->capacity); // Set the bit to the object
phBit_flags->size--; // Keep count of how many bits have been set
stat = SUCCESS;
printf("Bit set\n");
return stat;
}
Status bit_flags_unset_flag(BIT_FLAGS hBit_flags, int flag_position)
{
Status stat;
int bit_to_set = flag_position;
int* temp;
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags;
while(flag_position >= phBit_flags->capacity)
{
phBit_flags->capacity *= 2;
temp = (int*)malloc(sizeof(int) * phBit_flags->capacity);
if(temp != NULL)
{
*temp = *(phBit_flags->bit);
free(phBit_flags->bit);
phBit_flags->bit = temp;
}
else
{
stat = FAILURE;
printf("Could not resize bit flags object.\n");
return stat;
}
}
flag_position /= phBit_flags->capacity;
phBit_flags->bit[flag_position] &= ~(1 << (bit_to_set%phBit_flags->capacity));
phBit_flags->size++;
stat = SUCCESS;
printf("Bit clear\n");
return stat;
}
int bit_flags_check_flag(BIT_FLAGS hBit_flags, int flag_position)
{
int bit = 0, bit_set = flag_position;
Bit_flags* phBit_flags = (Bit_flags*)hBit_flags;
if(flag_position < 0 || flag_position > phBit_flags->capacity) // If the requested check is out-of-bounds
bit = -1; // Return error
else // Return bit requested
{
flag_position /= phBit_flags->capacity;
if((phBit_flags->bit[flag_position] >> bit_set) & 1)
{
bit = phBit_flags->bit[flag_position];
}
}
return bit;
}
int bit_flags_get_size(BIT_FLAGS hBit_flags)
{
Bit_flags* phBit_flags = (Bit_flags*) hBit_flags;
return phBit_flags->size;
}
int bit_flags_get_capacity(BIT_FLAGS hBit_flags)
{
Bit_flags* phBit_flags = (Bit_flags*) hBit_flags;
return phBit_flags->capacity;
}
void bit_flags_destroy(BIT_FLAGS* phBit_flags)
{
Bit_flags* pphBit_flags = (Bit_flags*) *phBit_flags;
if(pphBit_flags != NULL)
{
free(pphBit_flags->bit);
free(pphBit_flags);
}
pphBit_flags = NULL;
printf("Bit flags object memory print destroyed!\n");
}
bit_flag.h
#ifndef BIT_FLAGS_H
#define BIT_FLAGS_H
#include "status.h"
typedef void* BIT_FLAGS;
BIT_FLAGS bit_flags_init_number_of_bits(int number_of_bits);
Status bit_flags_set_flag(BIT_FLAGS hBit_flags, int flag_position);
Status bit_flags_unset_flag(BIT_FLAGS hBit_flags, int flag_position);
int bit_flags_check_flag(BIT_FLAGS hBit_flags, int flag_position);
int bit_flags_get_size(BIT_FLAGS hBit_flags);
int bit_flags_get_capacity(BIT_FLAGS hBit_flags);
void bit_flags_destroy(BIT_FLAGS* phBit_flags);
#endif
Come up with one proposed function that you could add to the interface (you do not have to write it) and explain why you think it should be added (what it would do and how it would be useful).
can you please help me with this. What other function can be use and explain why it is useful. Thank you. ( you do not need to write the code )

Step by stepSolved in 5 steps

- Scala programmingarrow_forwardC programming code for binary searcharrow_forwardWrite a function getNeighbors which will accept an integer array, size of the array and an index as parameters. This function will return a new array of size 2 which stores the neighbors of the value at index in the original array. If this function would result in returning garbage values the new array should be set to values {0,0} instead of values from the array.arrow_forward
- Submission (1) Convert the following pseudo code into assembly code. ( 7/' represents comments) // Each element is 1 Byte long // Each element is 1 Byte long arrayl 13h, 14h, 15h, 16h array2 12h, 13h, 14h, 15h // lengthl is a Symbolic constant // length2 is a Symbolic constant // this variable is 1 byte long initialized with 30 lengthl = number of items in Arrayl. length2 = number of items in Array2 samplel = 30h sample2 = 5h. // this variable is 1 byte long 1 byte long variable max of lengthl and length2. // maxlength is // this is a variable initialized with 0 maxlength = index = 0. While ( index array2[index] expl = (arrayl[index] * samplel) / (array2 [index] 1,1, only store the quotient of the division in expl and expl is Sample2) 16 bit long variable else expl = 0 index = index + 1 }arrow_forwardC programmingarrow_forwardC programmingarrow_forward
- c++ language using addition, not get_sum function with this pseudocode: Function Main Declare Integer Array ages [ ] Declare integer total assign numbers = [ ] assign total = sum(ages) output "Sum: " & total end fucntion sum(Integer Array array) declare integer total declare integer index assign total = 0 for index = 0 to size(array) -1 assign total = total + array[ ] end return integer totalarrow_forwardC++ Coding: Arrays Implement a two-dimensional character array. Use a nested loop to store a 12x6 flag. 5 x 2 stars as * Alternate = and – to represent stripe colors Use a nested loop to output the character array to the console.arrow_forwardusing accessarrow_forward
- Game of Hunt in C++ language Create the 'Game of Hunt'. The computer ‘hides’ the treasure at a random location in a 10x10 matrix. The user guesses the location by entering a row and column values. The game ends when the user locates the treasure or the treasure value is less than or equal to zero. Guesses in the wrong location will provide clues such as a compass direction or number of squares horizontally or vertically to the treasure. Using the random number generator, display one of the following in the board where the player made their guess: U# Treasure is up ‘#’ on the vertical axis (where # represents an integer number). D# Treasure is down ‘#’ on the vertical axis (where # represents an integer number) || Treasure is in this row, not up or down from the guess location. -> Treasure is to the right. <- Treasure is to the left. -- Treasure is in the same column, not left or right. +$ Adds $50 to treasure and no $50 turn loss. -$ Subtracts…arrow_forwardThe C code that returns the mean of the 10 float number array according to a decimal array and the number of elements sent into itarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





