比特反轉逆轉位的整數x
我發現一種用於在整數x反轉比特這個代碼(假設32位值):
unsigned int
reverse(register unsigned int x)
{
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return((x >> 16) | (x << 16));
}
我無法瞭解邏輯/該代碼背後的算法。所有幻數的目的是什麼?
http://stackoverflow.com/questions/746171/best-algorithm-for-bit-reversal-from-msb-lsb -to-LSB-MSB-在-C – StuartLC