-2

the following main program:

#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("\n");
    return 0;
}

Write the code for the definition of set_flag and check_flag so that the output of your program looks like the following: You can think of the set_flag function as taking an integer and making sure that the nth 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

the output should be

1000 0000 0000 0001 0000 0000 000 1000

I dont understand the bold part of the question.

can someone explain the problem to me. So am supposed to check the nth bit of the integer. however the initial integer is 0. means that no nth bit has a value of 1. so What am i supposed to do here?

Iluvatar
  • 1,537
  • 12
  • 13
Killer_B
  • 61
  • 4
  • 9
  • 1
    By "making sure", they don't mean confirming. They mean changing things such that it is the case. Also, oddly, they don't mean the n'th bit. They clearly intend `set_bit(&flag_holder, 3);` to set the fourth bit. – David Schwartz Dec 05 '16 at 19:21
  • so am supposed to change the nth bit to a 1. and then returning the integer of that binary value? – Killer_B Dec 05 '16 at 19:22
  • No. That function doesn't return anything. You are supposed to modify the value the first parameter points to. (And, oddly, it's the (n+1)'th bit you're supposed to set.) – David Schwartz Dec 05 '16 at 19:23
  • `set_bit` doesn't return anything (void), so just set it and you're done. – Iluvatar Dec 05 '16 at 19:23
  • yeah thats what i meant sorry – Killer_B Dec 05 '16 at 19:24
  • `set_flag()` (unposted) presumably sets the required bit. `check_flag()` (unposted) presumably returns `0` or `1` to show the state of the specified bit. – Weather Vane Dec 05 '16 at 19:27
  • You might be interested in this [post](http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c) – tabs_over_spaces Dec 05 '16 at 19:32

1 Answers1

1

They mean that the set_flag function modifies the value pointed to by its first parameter, such that the (n+1)'th bit is set.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278