#include <stdio.h>
unsigned int reverseBits(unsigned int num) {
unsigned int count = sizeof(num) * 8 - 1;
unsigned int reverse_num = num;
num >>= 1;
while (num) {
reverse_num <<= 1;
reverse_num |= num & 1;
num >>= 1;
count--;
}
reverse_num <<= count;
return reverse_num;
}
int main() {
unsigned int num = 10; // 00000000000000000000000000001010
unsigned int reversed_num = reverseBits(num);
printf("Original number: %u\n", num);
printf("Reversed number: %u\n", reversed_num);
return 0;
}
這段代碼實現了一個函數reverseBits
,用于將一個無符號整數中的二進制位逆序。在main
函數中,我們定義了一個初始值為10的無符號整數,將其二進制位翻轉后輸出。