如何找到C中不同的第一位?

JHn*_*net 7 c bit-manipulation

我想比较C中的整数,问题是找到不同的最低有效位.C中最快的方法是什么?

例:

               Bit
               3210
               ----
a = 13 (binary 1101)
b = 9  (binary 1001)
                ^
Run Code Online (Sandbox Code Playgroud)

这里的结果应该是2,因为第2位是不同的第一位.

Mar*_*n R 10

ffs()from <strings.h>返回第一个位集的位置,其中位从1最低有效位开始编号(并ffs(0)返回零):

unsigned a = 0x0D;
unsigned b = 0x09;

unsigned x = a ^ b;
int pos = ffs(x) - 1;
if (pos == -1) {
   // a and b are equal
} else {
   // pos is the position of the first difference 
}
Run Code Online (Sandbox Code Playgroud)