问题描述
我(仍在)通过bit hacks中的某些步骤,并全部组装完成,例如上一个问题Check if a number is even。
这里是我要切换的地方:
toggle_bit:
# NUM XOR (1 <<POS),pos is 1-indexed
# Note:
# NUM xor 0 = NUM
# NUM xor 1 = not(NUM),i.e.,flip all bits
mov $1,%eax # the bit to shift (pos-1)
lea -1(%esi),%ecx # shift amount -- pos-1
shl %cl,%rax # Now we have the 1 at the correct bit position
xor %rdi,%rax
ret
并称为:
mov $0b101,%edi # (5)
mov $2,%esi # (toggle 2nd bit)
call toggle_bit # return 0b111 (7)
这是建议的切换方式吗?还是应该使用btc
指令?
更新:这是我使用btc
改进后的功能:
toggle_bit:
mov %rdi,%rax # move the num into the return register
dec %sil # so pos it's one-indexed
btc %rsi,%rax
ret
这是一条比第一种方法短的指令-一种方法比另一种方法“更好”。如果是这样,为什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)