简体   繁体   中英

Bitwise setting in C++

#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 .

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.

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.

Is the following what you want:

void set(int val)
{
    support &= ~OUTGOING_MASK;
    support |= REFER_SUPPORTED;

    if(val == 1)
    {
        support |= OUTGOING_DISABLED;
    }
    else if(value == 2)
    {
        support |= OUTGOING_ENABLED;
    }
    else if(value == 3)
    {
        support |= OUTGOING_AUTO;
    }
}

If that is the case, then I believe you getter function is also wrong. According to my understanding, it should be as follows:

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM