简体   繁体   English

C#中单个&符号的第二个含义是什么?

[英]What is the second meaning of a single ampersand in C#?

I have used the single ampersand (&) in C# to mean "check the second conditional statement even if the first is false ". 我在C#中使用单个&符号来表示“检查第二个条件语句,即使第一个条件语句为 ”。

But the following seems to be a different meaning of & altogether, can anyone explain how i & 1 works in the following example? 但以下似乎是一个不同的含义 &完全,任何人都可以解释如何i & 1在下面的例子中的作品?

List<int> scores = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
var evenScores = scores.Where(i => i % 2 == 0);
var evenScores2 = scores.Where(i => (i & 1) == 0);

A single & is "Bitwise AND operator", just like dove said. 单个&是“按位AND运算符”,就像鸽子说的那样。 I'm looking at second part of question: "why it works?" 我正在看问题的第二部分:“它为什么会起作用?”

Think in binary: 想想二进制:

 000 = 0
 001 = 1
 010 = 2
 011 = 3
 100 = 4
 101 = 5
 110 = 6
 111 = 7
 and so on

Note all even numbers ends with 0; 注意所有偶数都以0结尾; so if last bit bitwise check against 1 returns zero (meaning "doesn't match"), its a even number; 因此,如果最后一位按1对1检查返回零(意思是“不匹配”),则为偶数;

Here : 这里

The unary & operator returns the address of its operand (requires unsafe context). 一元& operator返回其操作数的地址(需要unsafe上下文)。

Binary & operators are predefined for the integral types and bool. 二进制& operators是为整数类型和bool预定义的。 For integral types, & computes the logical bitwise AND of its operands. 对于整数类型, &计算其操作数的逻辑按位AND For bool operands, & computes the logical AND of its operands; 对于bool操作数, &计算其操作数的逻辑AND ; that is, the result is true if and only if both its operands are true. 也就是说,当且仅当它的两个操作数都为真时,结果才为真。

The & operator evaluates both operators regardless of the first one's value. & operator评估两个运算符,而不管第一个运算符的值。

Prior answers are true but don't address how & differs from && , which I thought was your original question, so I'll take that. 先前的答案是正确的,但没有解决&&问题&不同,我认为这是你原来的问题,所以我会接受。

As has been said, & is a bitwise AND. 如前所述, &是一个按位AND。 && is a logical AND. &&是一个合乎逻辑的AND。 & performs an AND operation on its operands bit by bit, and in general functions exactly like + or * or any arithmetic operator. &对其操作数逐位执行AND运算,一般函数与+*或任何算术运算符完全相同。 && is more complex. &&更复杂。 It compares each of its operands against zero. 它将每个操作数与零进行比较。 If the first operand is zero, it assumes the value false and short-circuits the rest of the expression, ie it does not evaluate any remaining operands. 如果第一个操作数为零,则它假定值为false并使表达式的其余部分短路,即它不评估任何剩余的操作数。 If the first value is non-zero, it examines the second value. 如果第一个值不为零,则检查第二个值。 If this is zero, it assumes the value of false , otherwise it assumes the value of true . 如果为零,则假定值为false ,否则假定值为true In either case, it continues to evaluate the expression. 在任何一种情况下,它都会继续评估表达式。

That is, there are two crucial differences between & and && : 也就是说, &&&之间存在两个重要区别:

  1. & operates bit by bit while && considers only zero and non-zero and always returns either 0 or 1. Thus 5 & 6 (binary 101 & 110 ) gives 4 (binary 100 ), while 5 && 6 gives 1 ( true ). &一点一点地操作,而&&只考虑零和非零,并且总是返回0或1.因此, 5 & 6 (二进制101 & 110 )给出4(二进制100 ),而5 && 6给出1( true )。

  2. && "short circuits". && “短路”。 If the first value is zero, it does not evaluate the second value. 如果第一个值为零,则不计算第二个值。 & has no such rule. &没有这样的规则。 This is important in several ways: 这在以下几个方面很重要:

    1. If the second value has any side effects, then with & those side effects always happen, while with && they do not. 如果第二个值有任何副作用,那么使用&那些副作用总会发生,而使用&&则不会。 So x & (y++) will always increment y , while x && (y++) will only increment y if x is not zero. 因此x & (y++)将始终递增y ,而x && (y++)仅在x不为零时才递增y This gets more important—and possibly more subtle—if the second operand is a function call. 如果第二个操作数是函数调用,这会变得更加重要 - 并且可能更微妙。
    2. The first value may test something that determines that the second value is invalid. 第一个值可能会测试确定第二个值无效的内容。 Like x!=NULL && x->foo==3 . x!=NULL && x->foo==3 With & , when x is null , that could bomb with segment faults or the equivalent. 使用& ,当xnull时,可能会出现段故障或等效故障。
    3. Lastly, there may be important performance gains. 最后,可能会有重要的性能提升。 Like, x!='A' && readTonsOfStuffFromDatabaseAndCalculateTotal(x) . 比如, x!='A' && readTonsOfStuffFromDatabaseAndCalculateTotal(x) With & , the read would happen regardless, and perhaps be a total waste of time. 使用& ,无论如何都会发生读取,也许是浪费时间。

That's why we almost always use && for things that really are logical operations, and limit use of & to when we truly want a bit-wise operation. 这就是为什么我们几乎总是使用&&来处理真正属于逻辑运算的事情,并且当我们真正想要一个按位操作时限制使用& But there are times when you DON'T want the short-circuit to happen, and in that case & may be a good choice. 但有时你不希望发生短路,在这种情况下&可能是一个不错的选择。 But if you're using it to operate "logically", be very careful with operands that can have any values other than 0 or 1. 1 && 2 is true , but 1 & 2 is false . 但是,如果您使用它来“逻辑”操作,请非常小心可以具有除0或1之外的任何值的操作数1 && 2true ,但1 & 2false

For boolean types, what you mentioned is how it behaves. 对于布尔类型,您提到的是它的行为方式。

For integer types, it's the bitwise "and" operator. 对于整数类型,它是按位“和”运算符。

It can be overloaded for other types. 它可以为其他类型重载。

Basically, the expression (i & 1) == 0 checks to see whether the least significant bit of i is set, which only happens if the number is odd. 基本上,表达式(i & 1) == 0检查是否设置了i的最低有效位,这仅在数字为奇数时才会发生。

The ampersand represents a bitwise AND operation. &符表示按位AND运算。 A bitwise operator returns the result of a comparison between each corresponding bit in the two operands. 按位运算符返回两个操作数中每个相应位之间的比较结果。

For example, if x is 0110 and y is 1010, then a bitwise AND of x and y (x & y) results in 0010. 例如,如果x是0110并且y是1010,则x和y(x&y)的按位AND导致0010。

按位AND运算符。

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

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