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

C language programing problem This daily will allow you to practice more with th

ID: 3889409 • Letter: C

Question

C language programing problem

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:

}

return 0;

Write the code for the definition of unset_flag and display_32_flags so that the output of your program looks like the following:

You can think of the unset_flag function as taking an integer and making sure that the nth 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 (just turn it into a function instead).

OCWindows system321cmd.exe 0000 0000 0000 0001 0000 0010 0000 0000 Press any key to continue.. _

Explanation / Answer

Given below is the competed code. Please do rate the answer if it helped. Thank you very much.

#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);


int main(int argc, char* argv[])
{

unsigned int flag_holder = 0;

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 0;
}

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

void set_flag(unsigned int* flag_holder, int flag_position)
{
int value = 1 << flag_position; //move 1 to specified bit position
*flag_holder = *flag_holder | value; //set the bit by ORing
}
int check_flag(unsigned 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;
}

void unset_flag(unsigned int * flag_holder, int flag_position)
{
unsigned int value = 1 << flag_position;
value = ~ value; //invert the bits, so all except the flag position are 1. the bit in flag postion is 0
*flag_holder = *flag_holder & value;//& the value so the flagposition is cleared
}

output

1000 0000 0000 0001 0000 0000 0000 1000
0000 0000 0000 0001 0000 0010 0000 0000