Search code examples
c#c++reverse-engineering

What does this mean in C# and how can I use it?


I've got the following code in C++ that I'm looking at in IDA Pro:

__int64 __fastcall sub_14023B480(__int64 a1)
{
  return *(_DWORD *)(a1 + 0x384) > 0 && !(*(_BYTE *)(a1 + 0xE8) & 8) && !(*(_DWORD *)(a1 + 0x290) & 0x800000);
}

a1 is an entity.

0x384 points to a health value.

0xE8 and 0x290 points to some kind of flag, but how come I can't write a check like this in C#:

if(!(a1 + 0xE8) & 8)
{
    //Do something
}

Since in C# the & operator returns in int value.


Solution

  • In C#, the ! operator cannot be applied to non-boolean types like this - a fact that is known to surprise both C and C++ programmers.

    To fix your flag check, you would write

    if (((a1 + 0xE8) & 8) == 0)
    {
        //Do something
    }
    

    All the brackets are required because of operator precedence. Without the "middle" bracket, "8 == 0" would be evaluated before the & and result in another compiler error, "Operator & cannot be applied to int and bool".