view plaincopy to clipboardprint?
/*
請編寫函數:
unsigned int reverse_bites(unsigned int value);
這個函數的返回值是把value的二進制位模式從左到右交換一次後的值。例如,在32位機器上,25這個值包含下列各個位:
00000000000000000000000000011001
函數的返回值應該是2,550,136,832,它的二進制位模式是:
10011000000000000000000000000000
編寫函數時要注意不要讓它依賴於你的機器上整型值的長度。
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
size_t reverse_bites(size_t value);
int main(void)
{
int input;
printf("Please input a number\n");
scanf("%d", &input);
printf("The result after reverse is %d\n", reverse_bites(input));
system("pause");
return 0;
}
size_t reverse_bites(size_t value)
{
int answer = 0;
for(int i = 1; i != 0; i <<= 1)
{
answer <<= 1;
if(value & 1)
answer |= 1;
value >>= 1;
}
return answer;
}
作者“xhz1234的專欄”