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: 3887438 • 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 3 include void setflag(unsigned int' flag_holder, int flag position); void unset flag(unsigned intflag holder, int flag position); int check flag(unsigned int flag holder, int flag_position); void display 32 flags (unsigned int flag holder); int main(int argc, char* argv[]) unsigned int flag holder-e; set flag(&flag; holder, 3) set flag(&flag; holder, 16): set flag(&flag; holder, 31): display 32 flags (flag holder); unset flag(&flag;_ holder, 31); unset flag(&flag;_ holder, 3); set flag(&Flag; holder, 9) display 32 flags (flag holder); return ; Write the code for the definition of unset flag and display 32 flags so that the output of your program looks like the following: r ss ary k. to continue You can think of the unset flag function as taking an integer and making sure that the n bit is a 0. You may find the operator useful. It is used to "flip the bits" of a number making all the zero values 1's and all the 1's zeroes. As in the previous daily, the shifting operators and the bitwise and (&) and or () may also be useful. If you are doing multiplication or division then you are doing it wrong. The display 32 flags function should just print the information to the screen as was given in the previous assignment Gust turn it into a function instead).

Explanation / Answer

Here is the code for 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_32_flags(unsigned int flag_holder);
void display_flags(unsigned int flag_holder[], int size);
int main(int argc, char* argv[])
{
unsigned int flag_holder = 0;
//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);
display_32_flags(flag_holder);
printf(" ");
unset_flag(&flag_holder, 31);
unset_flag(&flag_holder, 3);
set_flag(&flag_holder, 9);
//set_flag(flag_holder, 100);
//display_flags(flag_holder, 5);
display_32_flags(flag_holder);
return 0;
}

void set_flag(unsigned int* flag_holder, int flag_position)
{
*flag_holder |= (1 << flag_position);
}
int check_flag(unsigned int flag_holder, int flag_position)
{
return (flag_holder >> flag_position) & 1;
}

void unset_flag(unsigned int* flag_holder, int flag_position)
{
*flag_holder &= (0 << flag_position);
}
void display_32_flags(unsigned int flag_holder)
{
    for(int i=31; i>=0; i--)
{
printf("%d", check_flag(flag_holder, i));
if(i%4 == 0)
{
printf(" ");
}
}
printf(" ");
}