void set flagint flag holder int flag position Snt check fla
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;
}
