//将二进制数逆序输出。比如6为000...0110,逆序后为0110...000 #include <stdio.h> unsigned int reverse_bit(int num) { int i; int bit; unsigned new_num = 0; for (i = 0; i < 32; i++) { bit = num & 1; //取出最后一位 new_num <<= 1; //新数左移 new_num =new_num | bit; //把刚取出的一位加到新数 num >>= 1; //原数右移,准备取第二位 } return new_num; } int main() { printf("%u\n", reverse_bit(0)); printf("%u\n", reverse_bit(2147483648)); printf("%u\n", reverse_bit(3)); printf("%u\n", reverse_bit(100)); printf("%u\n", reverse_bit(6)); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-08 16:36:57