tags:

views:

116

answers:

4
#define OUTGOING_MASK         0x0c
#define OUTGOING_DISABLED     0x04
#define OUTGOING_ENABLED      0x08
#define OUTGOING_AUTO         0x00
#define REFER_SUPPORTED       0x80 

Assume support is some value of type int. I have a getter function

int get()
{
if(OUTGOING_DISABLED == support & OUTGOING_MASK)
return 1;
else if(OUTGOING_ENABLED == support & OUTGOING_MASK)
return 2;
else if(OUTGOING_AUTO == support & OUTGOING_MASK)
return 3;
}

I need to write set function for this like

void set(int val)
{
if(val ==1)
//todo
else if(value == 2)
//todo
else if(value == 3)
//todo
}

How to write getter and setter functions for this? I need to get/set the support variable here

REFER_SUPPORTED will always be set in support.

+2  A: 

You don't. In general, you can't recover that info given only a1 and a2. To see this, consider the case of a2 == 0. b & 0 is always 0.

Matthew Flaschen
Thanks Matthew. In my code, a2 can never be zero.It will be 3 or 4 generally. In that case, how to do? But a1 can be zero
Sunil
Matthew Flaschen
Still can't be done. Why do you feel you need this?
Artelius
A: 

You could use the following code to print out the binary equivalent

void printBit(int n)
{
  unsigned int i;
  i = 1<<(sizeof(n) * 8 - 1);

  while (i > 0)
  {
     if (n & i)
     {  
       printf("1");
     }
     else
     { 
        printf("0");
     }
    i >>= 1;
}
}

That would simply print out the binary equivalent of 'b'. Is that what you want to do?

IntelliChick
No. I dont want binary equivalent of b. I just want the value of b
Sunil
I think you need to be a bit more clear about what you mean by 'value'.
IntelliChick
+5  A: 

I have a statement such as a1 = b & a2; How to know the value of b using bitwise operators?

You can't recover value of b, unless a has ALL bits set. "&" is irreversible.

Explanation. & operation has following table:

a   b   result
1 & 1 = 1
0 & 1 = 0
1 & 0 = 0
0 & 0 = 0

which means, to recover b, you could try to use following table:

a   result  b
0   0       unknown - could be 1 or 0
0   1       invalid/impossible - could not happen
1   0       0
1   1       1

As you can see it isn't possible to guess b in all cases.

In expression a & b = c, if you know c and a, you can't recover b, because for every zeroed bit of c, and if corresponding bit of a is also zero, there are two possible states of corresponding bits of b. You can reliably recover b only if every bit of a is set to 1.

SigTerm
+1  A: 
Donotalo
Yeah...but i have another one called #define REFER_SUPPORTED 0x80which will always be set in support
Sunil
Please check my updated answer. I was wrong previously.
Donotalo
Thanks. can you write getter also according to the setter.
Sunil
your getter looks alright. i was wrong when i wrote the getter function.
Donotalo