首页 > 解决方案 > 取消设置 asm 中的最低设置位

问题描述

以下是我想出的取消设置数字中最低 1 位的功能。

unset_lowest_bit:
    # NUM & (NUM-1)
    # All binary digits to the left of the first 1 will remain unchanged, so & itself=itself
    # Any zeros to the right of the first 1 will stay zero, since anything & 0 = 0
    # And finally, the first 1 will go to zero, since the -1 will eventually need to borrow
    # Up to the first 1 digit
    lea -1(%rdi), %rax
    and %rdi, %rax
    ret

除了不为此使用函数调用之外,这是取消设置最低位的好实现吗?或者是否有这样做的指令(这似乎是一件非常不标准的事情,所以找不到任何指令)。

标签: assemblyx86bit-manipulation

解决方案


有一个指令,但它不在基本指令集中,它在 BMI1 中:blsr

BMI1 在 Intel 端(不包括 Atom)由 Haswell 和更新版本实现,在 AMD 端由 Jaguar&Piledriver 和更新版本实现。


推荐阅读