位域反转(Bit Reversal)
unsigned int reverse_bits(unsigned int num) {
unsigned int numOfBits = sizeof(num) * 8;
unsigned int reverseNum = 0;
for (unsigned int i = 0; i < numOfBits; i++) {
if (num & (1 << i)) {
reverseNum |= (1 << ((numOfBits - 1) - i));
}
}
return reverseNum;
}
该函数将给定的无符号整数的位进行反转,可以用于某些嵌入式系统中的位级操作需求。 |