How do you perform a BitWise AND on 2 32 bit integers in C#?
Since when is var a C# keyword?
Seva Alekseyev
2009-12-18 16:44:43
Since C# 3.0 -> http://msdn.microsoft.com/en-us/library/bb383973.aspx
Jan
2009-12-18 16:47:48
In other words, since 2006.
Joel Mueller
2009-12-18 16:55:04
@Joel - C#3.0 was released in November 2007 alongside the 3.5 framework
Lee
2009-12-18 17:08:04
My mistake, I presumed that C# 3.0 was released at the same time as .NET 3.0, in November 2006. Why would they ship C# 3.0 with .NET 3.5 but not .NET 3.0? http://en.wikipedia.org/wiki/.NET_Framework
Joel Mueller
2009-12-19 08:47:02
A:
int a = 42;
int b = 21;
int result = a & b;
For a bit more info here's the first Google result:
http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx
ruibm
2009-12-18 16:41:13
A:
var result = (UInt32)1 & (UInt32)0x0000000F;
// result == (UInt32)1; // result.GetType() : System.UInt32
If you try to cast the result to int, you probably get an overflow error starting from 0x80000000, Unchecked allows to avoid overflow errors that not so uncommon when working with the bit masks.
result = 0xFFFFFFFF; Int32 result2; unchecked { result2 = (Int32)result; }
// result2 == -1;
George Polevoy
2009-12-18 16:59:43