[英]Bitwise AND on 32-bit Integer
與&運算符
var x = 1 & 5;
//x will = 1
const uint
BIT_ONE = 1,
BIT_TWO = 2,
BIT_THREE = 4;
uint bits = BIT_ONE + BIT_TWO;
if((bits & BIT_TWO) == BIT_TWO){ /* do thing */ }
使用&運算符(不是&&)
int a = 42;
int b = 21;
int result = a & b;
有關更多信息,這是Google的第一個結果:
http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx
var result = (UInt32)1 & (UInt32)0x0000000F;
// result == (UInt32)1;
// result.GetType() : System.UInt32
如果嘗試將結果強制轉換為int,則可能會收到從0x80000000開始的溢出錯誤,而Unchecked可以避免使用位掩碼時不太常見的溢出錯誤。
result = 0xFFFFFFFF;
Int32 result2;
unchecked
{
result2 = (Int32)result;
}
// result2 == -1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.