简体   繁体   English

C#和运营商澄清

[英]C# & operator clarification

I saw a couple of questions here about the diference between && and & operators in C#, but I am still confused how it is used, and what outcome results in different situations. 我在这里看到了一些关于C#中&&和&运算符之间差异的问题,但我仍然对它的使用方式感到困惑,以及在不同情况下会产生什么结果。 For example I just glimpsed the following code in a project 例如,我只是在项目中瞥见了以下代码

bMyBoolean = Convert.ToBoolean(nMyInt & 1);
bMyBoolean = Convert.ToBoolean(nMyInt & 2);

When it will result 0 and when >0? 当它结果为0并且> 0时? What is the logic behind this operator? 这个运营商背后的逻辑是什么? What are the diferences between the operator '|'? 运营商'|'之间有什么区别?

bMyBoolean = Convert.ToBoolean(nMyInt | 1);
bMyBoolean = Convert.ToBoolean(nMyInt | 2);

Can we use the &&, || 我们可以使用&&,|| operators and get the same results (possibly with different code)? 运算符并获得相同的结果(可能使用不同的代码)?

The && is a conditional and used in if statements and while &&是一个条件,用于if语句和while

if(x>1 && y<3)

this means that x should be greater than 1 and y less than 3, satisfy both conditions 这意味着x应大于1且y小于3,满足两个条件

if(x>1 || y<3)

satisfy one of them 满足其中一个

However, & and | 但是,&和| are bitwise AND and OR respectively. 分别是按位AND和OR。 ex: 例如:

 1 | 0  => 1
 1 & 0  => 0
 1 & 1  => 1

if this apply for straight integers, their corresponding binary value will be calculated and applied 如果这适用于直整数,则将计算并应用其对应的二进制值

2&1
=>   10  // the binary value of 2
     &
     01  // the binary value of 1
     --
     00  // the result is zero

The ampersand does bitwise AND on the integers in their binary representations. &符号在二进制表示中对整数执行按位AND运算。 The pipe does bitwise OR. 管道按位OR。

See here what those bitwise operations mean: http://en.wikipedia.org/wiki/Bitwise_operation 在这里看一下这些按位操作意味着什么: http//en.wikipedia.org/wiki/Bitwise_operation

& and | &和| is bit operations. 是位操作。 You must use it on bit masks. 你必须在位掩码上使用它。 && and || &&和|| is logical operations so you can use it for only bool values. 是逻辑运算,因此您只能将其用于bool值。

Example of bit operation: 位操作示例:

var a = 1;
var b = 2;
var c = a|b;

in binary format this means a = 00000001, b = 00000010 c = 00000011 在二进制格式中,这意味着a = 00000001,b = 00000010 c = 00000011

So if you use bitmask c it will pass values 1, 2 or 3. 因此,如果使用位掩码c,它将传递值1,2或3。

另一个区别是&运算符计算其操作数的逻辑按位AND,如果操作数不是bool(在你的情况下是整数)

& operator is BItwise AND operator,it does manipulation on bits. & operator is BItwise AND运算符,它对位进行操作。 eg 5 & 3 例如5和3

        0101    //5
        0011   //3
    ----------
5&3=    0001   //1

| operator is BItwise OR | operator is BItwise OR operator,it does manipulation on bits. | operator is BItwise OR运算符,它对位进行操作。 5|3 5 | 3

        0101    //5
        0011   //3
    ----------
  5|3=  0111   //7

&& operator is logical AND operator - it returns true if all conditions are true &&运算符是logical AND operator - returns true if all conditions are truereturns true if all conditions are true
eg 例如

       if((3>5)&&(3>4))   //returns true
       if((6>5)&&(3>4))   //returns false

|| operator is logical OR operator - it returns true if one of the conditions is true operator是logical OR operator - returns true if one of the conditions is truereturns true if one of the conditions is true
eg 例如

   if((3>5)||(3>4))   //returns true
   if((6>5)||(3>4))   //returns true
   if((6>5)||(5>4))   //returns false

Other answers explains for you the different between && and &, so assume you understand this. 其他答案为您解释了&&和&之间的不同,所以假设您理解这一点。 In here, I just try to explain your specified case. 在这里,我只是试着解释你指定的情况。

First case 第一个案例

bMyBoolean = Convert.ToBoolean(nMyInt & 1);

bMyBoolean false when nMyInt = 0 because: bMyBoolean falsenMyInt = 0 ,因为:

  00 
& 01 
= 00;

Second case: 第二种情况:

bMyBoolean = Convert.ToBoolean(nMyInt & 2);

bMyBoolean false when nMyInt = 0 or 1 because bMyBoolean falsenMyInt = 01 ,因为

  00 
& 10 
= 00;

Or: 要么:

  01 
& 10 
= 00;

The third and fourth cases with bitwise | 第三和第四种情况是按位| are trivial because bMyBoolean always true with any nMyInt 是微不足道的,因为bMyBoolean对任何nMyInt始终为true

bMyBoolean = Convert.ToBoolean(nMyInt | 1);
bMyBoolean = Convert.ToBoolean(nMyInt | 2);

You cannot apply && or || 您不能应用&&或|| in this case because they are constraint only for bool , you will compiled errors. 在这种情况下,因为它们只是bool约束,你将编译错误。

Here is something interesting for & . 下面是一些有趣的事情。 bit-wise as & be, it can be used to bool as in example below. 按顺序,它可以用于bool,如下例所示。

bool result = true;
result &= false;
Console.WriteLine("result = true & false => {0}", result );
//result = true & false => False

result = false;
result &= false;
Console.WriteLine("result = false & false => {0}", result );
//result = false & false => False


result = true;
result &= true;
Console.WriteLine("result = true & true => {0}", result );
//result = true & true => True

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

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