Search code examples
javabitwise-operatorsdeobfuscation

How would this be deobfuscated [conditional statement with bitwise exclusive or]


Whats the equivalent of this without ixor ~

if(~(i3 + -1) < -1) { ... }

.

would it be this?

if((i3 + 1) > 0)  { ... }

or (doubt this?)

if((i3 + 0) > 0)  { ... }

or (doubt this?)

if(i3 < -1)  { ... }

Thanks I cannot really test it out myself well I can.. but I'm writing a deobfuscator and I want to be 100% sure.


Solution

  • ~x is the bitwise (not logical) inversion of x. In two's complement, it is equal to -1 - x. Try it.

    ~0  = -1 -  0 = -1
    ~-1 = -1 - -1 = 0
    ~1  = -1 -  1 = -2
    

    Now, to apply this to your condition:

    -1 - (i3 - 1) < -1
    -1 - i3 + 1   < -1   # commutative property of multiplication
       - i3       < -1   # 1 + -1 == 0
         i3       >  1   # * -1 inverts everything, including the inequality
    

    Note, this only applies if the numbers are two's complement -- but almost all CPUs (and most programming languages) behave that way these days.