C# Bitwise AND WTF
using System; namespace BitwiseAndWTF { class MainClass { public static void Main (string[] args) { byte b1 = 0x01; byte b2 = 0x02; byte b3 = b1 & b2; } } }
The above program WILL NOT COMPILE. Why not? Because b3
is of type byte
, and you can't assign it the value of an int
. That's right. A bitwise AND on two byte
s gives you an int
.
Why does this happen? I don't know, but I can guess. Section 14.10.1 of the C# specification suggests that the bitwise operators are only defined for int
, uint
, long
, and ulong
. Presumably, the byte
variables are being automatically promoted to int
. Thus, the result is an int
.
Bonus WTF: byte
is unsigned. int
is signed.