I understand that in C-like languages I can perform a boolean not with the not
operator: (C)
int myBool = TRUE;
myBool = !myBool;
But my question is: How is this implemented behind the scenes? My guess is by using jumps, but those could be inefficient if used excessively: (Intel x86 syntax)
; assume eax holds boolean
test eax, eax
jnz short boolTrue
inc eax ; eax was 0, now 1
jmp short after
boolTrue: ; eax non-zero
xor eax, eax ; eax now 0
after:
As shown, it requires 5 instructions with at least one jump and one bit-wise and
(test
). There has to be an easier way to do this because I've seen code bases that do "double-nots" (if (!!fileHandle)
) for some weird reason.
So (as said above): How do compilers do boolean !
s on x86?