我正在研究一个函数,它以64位整数作为参数,并返回一个具有所有设置位的64位整数。
01011001 -> 00001111 // examples
00010100 -> 00000011
我首先考虑了以下算法:
nb_ones = countSetBit(x)
int64 res = 1
for i from 1 to nb_ones+1:
res |= (1 << i)
这里的countSetBit
是在此处定义的。
有没有更加简单明了的方法?我正在使用C++编程。
countSetBit
是一种非常直接的方法。 - apple apple