On how to output a number as original code, inverse code and complement code

Posted by tested_123 on Tue, 12 Nov 2019 21:01:16 +0100

There is a question:

Write a function to input a number and output the original code, inverse code and complement code of the number

At first, I didn't use the idea of bit operation, so I wrote the following long code:
(suppose I type - 1754)

#include <stdio.h>
#include <math.h>

int positive(int num) //Used to calculate positive numbers
{
    int i,a[1000],b=1,c;
    for (i=0; i<1000; i++) {
        a[i]=num%2;
        num=num/2;
        if (num<1)
        {
            b=i;
            break;
        }
    }
    a[b+1]=0;
    printf("ture code is ");
    for (i=b+1; i>=0; i--)
    {
        printf("%d",a[i]);
    }
    printf("\nones-complement code is ");
    for (i=b+1; i>=0; i--) {
        c=a[i];
        c=~c&1;   //Must have, or it will be 11111111 c
        printf("%d",c);
    }
    printf("\ncomplemental code is ");
    for (i=b+1; i>=0; i--)
    {
        printf("%d",a[i]);
    }
    printf("\n");
    return 0;
}

int negetive(int num)  //Negative calculation
{
    int i,a[1000],d[1000],b=1,c,e = 0,numb;
    numb=num;
    num=-num;
    for (i=0; i<1000; i++) {
        a[i]=num%2;
        num=num/2;
        if (num<1)
        {
            b=i;
            break;
        }
    }
    num=-numb;
    num=~num;  //Here, the num output will appear - 1755, because the current machine generally uses complement to represent negative numbers
    num=num+1;
    printf("%d\n",num);
    for (i=0; i<1000; i++) {//Note that the for loop is wrong here
        d[i]=num%2;
        num=num/2;
        if (num>-1)
        {
            e=i;
            break;
        }
    }
    a[b+1]=1;
    d[e+1]=1;
    printf("ture code is ");
    for (i=b+1; i>=0; i--)
    {
        printf("%d",a[i]);
    }
    a[b+1]=0;
    printf("\nones-complement code is ");
    for (i=b+1; i>=0; i--) {
        c=a[i];
        c=~c&1;   //It has to be or it will be 11111111
        printf("%d",c);
    }
    printf("\ncomplemental code is ");
    for (i=e+1; i>=0; i--)
    {
        printf("%d",d[i]);
    }
    printf("\n");
    return 0;
}

int main()  //main program
{
    int num;
    printf("please input a number:\n");
    scanf("%d",&num);
    if (num>0) {
        positive(num);
    }
    else if (num==0)
        printf("0000000000000000\n1111111111111111\n0000000000000000");
    else
        negetive(num);
}

Without the introduction of bit operation, the whole code will be lengthy, but it doesn't matter if it's complicated, just right.
But he's not right.. There is no problem with the output of positive numbers. Once a negative number is entered, its complement will be wrong.
Why? Let's take a look at the negative for cycle separately:

num=-numb; //The first step is to change num back to 1754
    num=~num;  //For the 1754 inverse code, the num output here will appear - 1755, because the current machine generally uses complement to represent negative numbers, so after the inverse code is calculated, the machine directly thinks that this is a complement code, and it will read according to the complement reading method
    num=num+1;  //Here is for - 1755 + 1 = - 1754
    for (i=0; i<1000; i++) //Note that the for loop is wrong here. Enter - 1754 into the loop
    {
        d[i]=num%2;  // It's all negative here
        num=num/2;
        if (num>-1)
        {
            e=i;
            break;
        }
    }  // Because it's a negative number, the output will go wrong later

So I simplified the binary output with bit operation writing method:

  for(i=15;i>=0;i--)  //Default 16 bits
        {
            b=a>>i;     //Read the last number one by one in each cycle
            b=b&~(~0<<1);   //xcode will alarm and prompt undefined left shift, which can be replaced by B = B & ~ (~ 0-1)
            printf("%hd",b); 
        }
    }

This code does not even need to distinguish positive and negative numbers. If it is a negative number, it is stored by complement by default, so it can be read directly.

Topics: xcode