我正在尝试理解一个汇编程序而没有任何解释:
;x is stored at 8(%ebp)
movl 8(%ebp), %edx
movl $0, %eax
testl %edx, %edx
je .L7
.L10:
xorl %edx, %eax
shrl %edx
jne .L10
.L7:
andl $1, %eax
我在C和Python中实现它:
unsigned fun_a(unsigned x) {
int val = 0;
while (x != 0) {
val = val ^ x;
x = x >> 1;
}
return val && 1;
}
def fun_a(x):
val = 0
while x != 0:
val = val ^ x
x = x >> 1
return val & 1
但结果却产生了不同的结果:
//C
fun_a(0) = 0
fun_a(1) = 1
fun_a(2) = 1
fun_a(3) = 1
fun_a(4) = 1
fun_a(5) = 1
fun_a(6) = 1
fun_a(7) = 1
fun_a(8) = 1
fun_a(9) = 1
#Python
fun_a(0) = 0
fun_a(1) = 1
fun_a(2) = 1
fun_a(3) = 0
fun_a(4) = 1
fun_a(5) = 0
fun_a(6) = 0
fun_a(7) = 1
fun_a(8) = 1
fun_a(9) = 0
我的C / Python程序出了什么问题?
答案 0 :(得分:0)
在c中你返回val&& 1,这是一个逻辑和。在python中,返回val& 1是按位的。