This daily will allow you to practice more with the bit wise operators and shift
ID: 3890462 • Letter: T
Question
This daily will allow you to practice more with the bit wise operators and shifts. Consider the following modification of the main program from daily 4 void set_flag(unsigned int flag_holder[], int flag_position); void unset flag(unsigned int flag holder[], int flag_position); int check flag(unsigned int flag_holder[, int flag position); void display_32 flags_as_array(unsigned int flag holder); void display flags (unsigned int flag holder[], int size); int main(int argc, char* argv[]) unsigned int flag holder[5]-;//set the first integer to zero and all others to zero by default. set_flag(flag holder, 3); set flag(flag_holder, 16) set_flag(flag holder, 31); set flag(flag_holder, 87); display flags (Flag_holder, 5) printf("Inn") unset flag(flag holder, 31); unset flag (flag_holder, 3); set flag(flag holder, 99) set flag(flag holder, 100): display flags (Flag_holder, 5) return ; Here I have changed the functions so that they take an array of integers instead of just one integer. This allows me to imagine that I have a long array of bits instead of an array of integers. The functions can now set, unset, check and display flags for any bit in the array of 5 integers that I have made (and should work for any size array as long as your bit index is in bounds of your array). I also changed the display behavior. Daily 4 displayed the flags as you would see them in a number but since this program is moving away from the idea of a binary number to binary store bits and moving toward the idea of having an array of bits the display_32_flags_as_array function will display the [0] bit first then and so on up to 31 whereas the display-32-flags function in daily 4 displays the [31 ] bit first and down to [0] Similarly the display_flags function now takes an array of integers and displays one integer per line using the display_32_ flags_as array function. Your output should look exactly like the followingExplanation / Answer
Given below is the code for the question along with output.
Please do rate the answer if it helped. Thank you.
#include <stdio.h>
void set_flag(unsigned int flag_holder[], int flag_position);
void unset_flag(unsigned int flag_holder[], int flag_position);
int check_flag(unsigned int flag_holder[], int flag_position);
void display_flags(unsigned int flag_holder[], int size);
void display_32_flags_as_array(unsigned int flagholder);
int main(int argc, char* argv[])
{
unsigned int flag_holder[5] = {0}; //set all to zeros
set_flag(flag_holder, 3);
set_flag(flag_holder, 16);
set_flag(flag_holder, 31);
set_flag(flag_holder, 87);
display_flags(flag_holder, 5);
printf(" ");
unset_flag(flag_holder, 31);
unset_flag(flag_holder, 3);
set_flag(flag_holder, 99);
set_flag(flag_holder, 100);
display_flags(flag_holder, 5);
return 0;
}
void display_flags(unsigned int flag_holder[], int size)
{
int i ;
for(i = 0; i < size; i++)
display_32_flags_as_array(flag_holder[i]);
}
void display_32_flags_as_array(unsigned int flag_holder)
{
int i ;
for(i = 0; i < 32; i++)
{
if(i % 4 == 0)
{
printf(" ");
}
printf("%d", check_flag(&flag_holder, i)); //since check flag expects array/address, we will pass address
}
printf(" ");
}
void set_flag(unsigned int flag_holder[], int flag_position)
{
int index = flag_position / 32; //find out which number in the array should be modified
int bit_position = flag_position % 32; // find out which bit in the selected number will be modified
int value = 1 << bit_position; //move 1 to specified bit position
flag_holder[index] = flag_holder[index] | value; //set the bit in the selected number at index
}
int check_flag(unsigned int flag_holder[], int flag_position)
{
int index = flag_position / 32; //find out which number in the array is requested
int bit_position = flag_position % 32; // find out which bit in the selected number
int value = 1 << bit_position; //move 1 to the specified position
int bit = flag_holder[index] & value; //extract the bit
if(bit == 0)
return 0;
else
return 1;
}
void unset_flag(unsigned int flag_holder[], int flag_position)
{
int index = flag_position / 32; //find out which number in the array is requested
int bit_position = flag_position % 32; // find out which bit in the selected number
int value = 1 << bit_position; //move 1 to the specified position
value = ~ value; //invert the bits, so all except the flag position are 1. the bit in flag postion is 0
flag_holder[index] = flag_holder[index] & value;//& the value so the bitposition is cleared
}
output
0001 0000 0000 0000 1000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0001 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 1000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0001 0000 0000
0001 1000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000