Skip to content

Instantly share code, notes, and snippets.

@jaredpar
Last active December 14, 2015 23:09
Show Gist options
  • Save jaredpar/5163187 to your computer and use it in GitHub Desktop.
Save jaredpar/5163187 to your computer and use it in GitHub Desktop.
Fun with booleans
class Program
{
[StructLayout(LayoutKind.Explicit)]
struct Union
{
[FieldOffset(0)]
internal byte ByteField;
[FieldOffset(0)]
internal bool BoolField;
}
static void Main(string[] args)
{
Union u1 = new Union();
Union u2 = new Union();
u1.ByteField = 1;
u2.ByteField = 2;
bool b1 = u1.BoolField;
bool b2 = u2.BoolField;
Console.WriteLine(b1 & b2); // False
Console.WriteLine(b1 && b2); // True
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment