#include <stdio.h> void origBinary(int num); void reverseBinary(int num); void buBinary(int num); int main() { int num = 0; printf("请输入1个整数:"); scanf("%d",&num); // num = ~num; printf("%d的原码、反码、补码如下:\n",num); origBinary(num); reverseBinary(num); buBinary(num);
return 0; }
void origBinary(int num) { // printf("%d的原码:",num); if(num >=0) { for (int j = 31; j >= 0; j--) { char ch = (num >> j) & 1;
(j+1) % 8 ==0 && (j != 31) ? printf(" "):0; printf("%d",ch); } } else { printf("1"); num = num - 1 ; for (int j = 30; j >= 0; j--) { char sh = (num >> j) & 1; sh = !sh; (j+1) % 8 == 0 && (j != 31)? printf(" "):0; printf("%d",sh); }
}
printf("\n"); }
void reverseBinary(int num) { //printf("%d的反码是:",num); if(num >=0) { origBinary(num); return; } else { printf("1"); num = num - 1; for (int j = 30; j >= 0; j--) { char ch = (num >> j) & 1; //ch = !ch; (j+1) % 8 ==0 && (j != 31) ? printf(" "):0; printf("%d",ch); }
} printf("\n"); }
void buBinary(int num) { if(num >=0) { origBinary(num); return; } else { for (int j = 31; j >= 0; j--) { char ch = (num >> j) & 1;
(j+1) % 8 ==0 && (j != 31) ? printf(" "):0; printf("%d",ch); } } printf("\n");
}
|