(运算符)& 运算符

  & 运算符既可作为一元运算符也可作为二元运算符。

  备注

  一元 & 运算符返回操作数的地址(要求 unsafe 上下文)。

  为整型和 bool 类型预定义了二进制 & 运算符。 对于整型,& 计算操作数的逻辑按位“与”。 对于 bool 操作数,& 计算操作数的逻辑“与”;也就是说,当且仅当两个操作数均为 true 时,结果才为 true。

  & 运算符计算两个运算符,与第一个操作数的值无关。 例如:

  
1 int i = 0;
2 if (false & ++i == 1)
3 {
4     // i is incremented, but the conditional
5     // expression evaluates to false, so
6     // this block does not execute.
7 }
View Code

  用户定义的类型可重载二元 & 运算符(请参见 operator)。 对于整数类型适用的运算对枚举类型通常也适用。 重载二元运算符时,也会隐式重载相应的赋值运算符(如果有)。

  示例

  
 1 class BitwiseAnd
 2 {
 3     static void Main()
 4     {
 5         // The following two statements perform logical ANDs.
 6         Console.WriteLine(true & false); 
 7         Console.WriteLine(true & true);  
 8 
 9         // The following line performs a bitwise AND of F8 (1111 1000) and
10         // 3F (0011 1111).
11         //    1111 1000
12         //    0011 1111
13         //    ---------
14         //    0011 1000 or 38
15         Console.WriteLine("0x{0:x}", 0xf8 & 0x3f); 
16     }
17 }
18 // Output:
19 // False
20 // True
21 // 0x38
View Code
原文地址:https://www.cnblogs.com/gamerLin/p/4446738.html