Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This daily will allow you to practice more with the bit wise operators and shift

ID: 3887326 • Letter: T

Question

This daily will allow you to practice more with the bit wise operators and shifts. Consider the following main program: #include void set flag(int' flag_holder, int flag position); int check flag(int flag holder, int flag_position); int main(int argc, char* argv[]) int flag-holder = e; int i; set flag(&flag; holder, 3); set flag(&flag;_holder, 16); set flag(&flag;_holder, 31); for (i-31;-e; i) printf(Xd", check flag(flag holder, i)): printf(" ") printf("n" return e Write the code for the definition of set flag and check flag so that the output of your program looks like the following: ress any key to cont inue You can think of the set flag function as taking an integer and making sure that the n bit is a 1. The check flag function simply returns an integer that is zero when the nth bit is zero and 1 when it is 1. You may find the shifting

Explanation / Answer

Given below is the completed program with output. If it helped, please don't forget to rate it . Thank you very much.

#include <stdio.h>

void set_flag(int* flag_holder, int flag_position);
int check_flag(int flag_holder, int flag_position);

int main(int argc, char* argv[])
{
int flag_holder = 0;
int i;

set_flag(&flag_holder, 3);
set_flag(&flag_holder, 16);
set_flag(&flag_holder, 31);

for(i = 31; i >= 0; i--)
{
printf("%d", check_flag(flag_holder, i));
if(i % 4 == 0)
{
printf(" ");
}
}

printf(" ");
return 0;
}


void set_flag(int* flag_holder, int flag_position)
{
int value = 1 << flag_position; //create a value with specified bit set
*flag_holder = *flag_holder | value; //change only the specified bit by ORing
}
int check_flag(int flag_holder, int flag_position)
{
int value = 1 << flag_position; //move 1 to the specified position
int bit = flag_holder & value; //extract the bit

if(bit == 0)
return 0;
else
return 1;

}

output

1000 0000 0000 0001 0000 0000 0000 1000