#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdio.h> char stry[33] = { 0 }; char strf[33] = { 0 }; char strb[33] = { 0 }; void bcode(int num) { unsigned int data = 1 << 31;//左移31位 for (int i = 1; i <= 32; i++) { if (num&data) { strb[i - 1] = '1'; } else { strb[i - 1] = '0'; } num <<= 1; } } void ycode(int num) { unsigned int data = 1 << 31;//左移31位 if (num < 0) { num = ~num + 1; num = num | data;//确保符号位是1 后面都不变 } for (int i = 1; i <= 32; i++) { if (num&data) { stry[i - 1] = '1'; } else { stry[i - 1] = '0'; } num <<= 1; } } void fcode(int num) { unsigned int data = 1 << 31;//左移31位 if (num < 0) { num = num - 1; } for (int i = 1; i <= 32; i++) { if (num&data) { strf[i - 1] = '1'; } else { strf[i - 1] = '0'; } num <<= 1; } } void main() { int num; scanf("%d", &num); ycode(num); bcode(num); fcode(num); printf("原码:%s", stry); printf(" 反码:%s", strf); printf(" 补码:%s", strb); system("pause"); }