当前位置:网站首页>Original code, inverse code, complement calculation function applet; C code implementation;

Original code, inverse code, complement calculation function applet; C code implementation;

2022-06-21 09:39:00 ah_ yl

We just need to keep one thing in mind ; All data is stored in the form of complement in the computer .
The following for c Language code implementation ;
It can be compiled directly , Use directly as a small application ;

#include <stdio.h>
int main()
{
    
    char a=0;
    printf("=============================================\n");
    printf(" Please input data (  Range  -128~127):\n");
    printf(" ");
    while(~scanf("%d",&a))
    {
    
        unsigned char value=0;
        unsigned char bitOri[8]={
    0};
        unsigned char bitInv[8]={
    0};
        unsigned char bitPack[8]={
    0};
        unsigned char bitDiv[8]={
    128,64,32,16,8,4,2,1};
        if(a>0)
        {
    
            // A positive number , The original code is equal to the inverse code and the complement ;
            value = a;
            int i;
            for(i=0;i<8;++i)
            {
    
                bitOri[i]=value/bitDiv[i];
                value=value%bitDiv[i];
            }
            memcpy(bitInv,bitOri,8);
            memcpy(bitPack,bitOri,8);
        }
        else
        {
    
            // When negative , Original code , The inverse code is the inverse of the original code divided by the sign bit , Complement is inverse +1
            value=-a;
            bitOri[0]=1;
            bitInv[0]=1;
            int i;
            for(i=1;i<8;++i)
            {
    
                bitOri[i]=value/bitDiv[i];
                bitInv[i]=!bitOri[i];
                value=value%bitDiv[i];
            }
            value=a;
            for(i=0;i<8;++i)
            {
    
                bitPack[i]=value/bitDiv[i];
                value=value%bitDiv[i];
            }
        }
        int i;
        if(a>-128)
        {
    
            printf(" The original code of the data is : \n");
            printf("\n");
            printf(" ");
            for(i=0;i<8;++i)
            {
    
                printf("%d ",bitOri[i]);
                if(i==3)printf(" ");
            }
            printf("\n");
            printf("\n");
            printf(" The inverse code of the data is : \n");
            printf("\n");
            printf(" ");
            for(i=0;i<8;++i)
            {
    
                printf("%d ",bitInv[i]);
                if(i==3)printf(" ");
            }
            printf("\n");
            printf("\n");
        }
        else
        {
    
            printf(" The original code of the data is : INVALID\n");
            printf(" The inverse code of the data is : INVALID\n");

        }
        printf(" The complement of the data is : \n");
        printf("\n");
        printf(" ");
        for(i=0;i<8;++i)
        {
    
            printf("%d ",bitPack[i]);
            if(i==3)printf(" ");
        }
        printf("\n");
        printf("\n");
        printf("=============================================\n");
        printf(" Please input data (  Range  -128~127):\n");
        printf(" ");
    }
    return 0;
}
原网站

版权声明
本文为[ah_ yl]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221443018398.html

随机推荐