好玩意兒
剛剛看到的一個有趣的算法,可以把X最右邊的1去除掉。例如:
01111111 127 01111110 126 127 & 126 = 01111110
01011000 88 01010111 87 88 & 87 = 01010000
若要應用嘛,可用在想找出有幾個bit on的情況:
int quickBitcount(unsigned x) { int count = 0; while (x) { x &= (x-1); count++; } return count; }
from:http://sevensavants.blogspot.com/2010/01/x-x-x-1-trick.html