#include <stdio.h>
int count_bits(unsigned x)
{
int count = 0;
while (x) {
if (x & 1U)
count++;
x >>= 1;
}
return count;
}
int int_bits(void)
{
return (count_bits(~0U));
}
void print_bits(unsigned x)
{
int i;
for (i = int_bits() - 1; i >= 0; i--)//这一句不懂,求解释
putchar(((x >> i) & 1U) ? '1' : '0');
return;
}
int main(void)
{
unsigned na, nb;
puts("please enter two integer");
printf("integer A: ");
scanf("%u", &na);
printf("integer B: ");
scanf("%u", &nb);
printf("\n A = ");
print_bits(na);
printf("\n B = ");
print_bits(nb);
printf("\n A & B = ");
print_bits(na & nb);
printf("\n A | B = ");
print_bits(na | nb);
printf("\n A ^ B = ");
print_bits(na ^ nb);
printf("\n ~A = ");
print_bits(~na);
printf("\n ~B = ");
print_bits(~nb);
putchar('\n');
return 0;
}
添加回答
举报
0/150
提交
取消