简体   繁体   English

C ++中的按位设置

[英]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. 假设support是int类型的一些值。 I have a getter function 我有一个getter功能

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 我需要为此编写set函数

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? 如何为此编写getter和setter函数? I need to get/set the support variable here 我需要在这里获取/设置support变量

REFER_SUPPORTED will always be set in support . REFER_SUPPORTED将始终设置为support

I have a statement such as a1 = b & a2; 我有一个声明,如a1 = b&a2; How to know the value of b using bitwise operators? 如何使用按位运算符知道b的值?

You can't recover value of b, unless a has ALL bits set. 除非设置了ALL位,否则无法恢复b的值。 "&" 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: 这意味着,要恢复b,您可以尝试使用下表:

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. 如你所见,在所有情况下都无法猜测b。

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. 在表达式a&b = c中,如果你知道c和a,则无法恢复b,因为对于c的每个零位,并且如果a的对应位也为零,则b的相应位有两种可能的状态。 。 You can reliably recover b only if every bit of a is set to 1. 只有当a的每个位都设置为1时,才能可靠地恢复b。

You don't. 你没有。 In general, you can't recover that info given only a1 and a2 . 通常,只有a1a2才能恢复该信息。 To see this, consider the case of a2 == 0 . 要看到这一点,请考虑a2 == 0的情况。 b & 0 is always 0. b & 0始终为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. 如果是这样,那么我相信你的getter功能也是错误的。 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'. 这只会打印出二进制等效的'b'。 Is that what you want to do? 那是你想做的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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