(运算符)& 运算符

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

  备注

  一元 & 运算符返回操作数的地址(要求 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值