1204: 华科版C语言程序设计教程(第二版)课后习题3.8

#include <stdio.h>
#include <math.h>


int main()
{
int mask[16] = { 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384 };
int x, m, n;
int number[16] = { 2 };
while (scanf_s("%d%d%d", &x,&m,&n) != EOF)
{
int i;
for (i = 0; i < n; i++)
{
//用掩码求出所需的位数
if ((x&mask[m + i]) != 0)
{
number[i] = 1;
}
else
{
number[i] = 0;
}


}
i--;
int w = 15;
int sum=0;
int j;
int temp = i;
//当输入为12345 0 16时,输出为12344,为什么number[1]没有加上去??
for (j = 0; j < i; j++)
//while(i--)
{
sum += number[temp--] * pow(2,w--);
}
printf("%d\n", sum);
}

}

猜你喜欢

转载自blog.csdn.net/RedemptionC/article/details/75000414