void set flagint flag holder int flag position Snt check fla

void set flag(int flag holder int flag position Snt check flag(int flag holder, int flag position int nain(int arge, char argvlj) int flag holder int i set flag(& flag holder, 3) set flag(&flag; holder 16) set flag &flag; holder, 31): for (i 31; i> e: i--) printf(\" check flag(flag holder, i) e) printf(\" print Write the code for the definition of set flag and check flag so that the output of your program looks like the following: sure that the bit a You can think of the set fag function as taking an inteper and making The function simply COMP1020 Day Daily Spring 2017 or division in your solution then you are doing it wrong. At the top of your code you should have a comment section that has the flowing format Author: your nane> etine you spent on this projector rpose of this assignment in your own words

Solution

#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)
{
// initialize a flag by 1
int flag = 1;
// shift this bit so that it is set at given flag position
flag = flag << (flag_position);

// binary operator \'|\' will set this bit
*flag_holder = (*flag_holder | flag);
}

int check_flag(int flag_holder, int flag_position)
{
// initialize flag by 1
int set_bit = 1;

// shift flag holder such that flag_position bit become last bit
flag_holder = flag_holder >> flag_position;

// logical operator will keep only last bit set if it is already set and unset all other bits
return (flag_holder & set_bit) > 0;
}

 void set flag(int flag holder int flag position Snt check flag(int flag holder, int flag position int nain(int arge, char argvlj) int flag holder int i set fla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site