static void Main(string[] args)
{
bool a, b;
unsafe
{
int* pa = (int*)&a;
int* pb = (int*)&b;
*pa = 1;
*pb = 2;
}
Console.Write(Test(a, b));
}
This prints True
for me with the C# implementation that comes with Visual Studio 2015. I actually don't know any C#, but I figured I'd try to write some C code and see if it worked. I was hoping that the compiler would assume that True is always represented as 1 and use a bitwise AND. In Debug mode, this is indeed the case (it worked with Release too). It uses a bitwise AND for the first condition and two comparisons to zero for the second:
if (a && b) return false;
002C2E92 movzx eax,byte ptr [ebp-3Ch]
002C2E96 movzx edx,byte ptr [ebp-40h]
002C2E9A and eax,edx
002C2E9C and eax,0FFh
002C2EA1 mov dword ptr [ebp-44h],eax
002C2EA4 cmp dword ptr [ebp-44h],0
002C2EA8 je 002C2EB2
002C2EAA xor edx,edx
002C2EAC mov dword ptr [ebp-48h],edx
002C2EAF nop
002C2EB0 jmp 002C2EE4
if (a) if (b) return true;
002C2EB2 movzx eax,byte ptr [ebp-3Ch]
002C2EB6 mov dword ptr [ebp-4Ch],eax
002C2EB9 cmp dword ptr [ebp-4Ch],0
002C2EBD je 002C2EDC
002C2EBF movzx eax,byte ptr [ebp-40h]
002C2EC3 mov dword ptr [ebp-50h],eax
002C2EC6 cmp dword ptr [ebp-50h],0
002C2ECA je 002C2EDC
002C2ECC mov eax,1
002C2ED1 and eax,0FFh
002C2ED6 mov dword ptr [ebp-48h],eax
002C2ED9 nop
002C2EDA jmp 002C2EE4
return false;
002C2EDC xor edx,edx
002C2EDE mov dword ptr [ebp-48h],edx
002C2EE1 nop
002C2EE2 jmp 002C2EE4
}
002C2EE4 mov eax,dword ptr [ebp-48h]
002C2EE7 lea esp,[ebp-0Ch]
002C2EEA pop ebx
002C2EEB pop esi
002C2EEC pop edi
002C2EED pop ebp
002C2EEE ret