Dr.*_*eon 5 c c++ 64-bit bit-manipulation
好吧,大家好,我知道我想做什么,但我不知道它是否已经存在(作为一个函数或理论上)或如何用它来表达,所以我需要你的帮助:
10101110(lsb)一个例子 :
因此,我们的虚函数CROPLEFT(X,POS)的最终结果将返回,其中X = 10101110,POS = 1 00001110.
有任何想法吗?
R..*_*R.. 12
小菜一碟.
y = ~x; // We like working with 1's, not 0's.
y &= -y; // Mask off all but the lowest-set bit
x &= y-1; // Make a mask for the bits below that and apply it.
Run Code Online (Sandbox Code Playgroud)
并添加了位置参数:
y = ~x & -1U<<pos; // Change 1U to a larger type if needed.
y &= -y;
x &= y-1;
Run Code Online (Sandbox Code Playgroud)
关键因素是第二行,并且您可以y通过应用逻辑和反对来替换仅具有最低设置位的值-y.可悲的是,除非你有一个特殊的cpu指令,否则没有获得最高设置位的运气,所以你很幸运,你的问题要求最低.