设计一个程序,要求如下

实现一个数据的输入(整形),转变为原码,再变成补码,按照计算机处理过程模拟,开发语言不限,要求可视化模拟细节过程。

给你一个C语言的实现把。 

#include <stdio.h>

void printBinary(int x){
    int l = sizeof(x) * 8;
    int mask = 0x80000000;
    for(int i = 0; i < l; i++){
        if(x&mask){
            printf("1");
        }
        else{
            printf("0");
        }
        x=x<<1;
    }
    printf("\n");
}

void printTrueCode(int x){
    if(x < 0){
        x = ~x+1;
    }
    printf("Print True Code. \n");
    printBinary(x);
}

int main()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", n);
    printTrueCode(n);
    printf("Print Complemental Code.\n");
    printBinary(n);
}

 

本来数字在计算机中存储的就是补码形式。如果想要获取他的原码,只需要当这个数字是负数的时候取反加一即可。

 

 

试一试~如果问题得到解决,记得采纳一波。