求数的补码 c语言中

2024-12-01 13:02:42
推荐回答(1个)
回答1:

简单的位运算就可以了
#include

void printBin(int num){
int i,k=31;
for(i=0;i<32;i++){
printf("%d",(num>>k)&1);//移位并输出
k--;
}
printf("\n");
}

int main(int argc, char* argv[])
{
int num;
printf("Please input a number, I will tell you what it really is:");
scanf("%d",&num);
printf("It is binary display is:");
printBin(num);
return 0;
}