(运算符) 运算符
& 運算符既可作為一元運算符也可作為二元運算符。
備注
一元 & 運算符返回操作數的地址(要求?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
總結
- 上一篇: POJ 2075 Tangled in
- 下一篇: 2021-02-16