我知道在类 C 语言中,我可以不使用运算符执行布尔not
运算:(C)
int myBool = TRUE;
myBool = !myBool;
但我的问题是:这是如何在幕后实现的?我的猜测是使用跳转,但如果过度使用这些可能效率低下:(英特尔 x86 语法)
; 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:
如图所示,它需要 5 条指令,其中至少有一个跳转和一个按位and
( test
)。if (!!fileHandle)
必须有一种更简单的方法来做到这一点,因为我已经看到代码库出于某种奇怪的原因执行“双重非”( )。
所以(如上所述):编译器如何!
在 x86 上执行 boolean ?