Use code to calculate original code, inverse code and complement code

When I reviewed digital logic recently, I saw this chapter and wanted to go online to find a calculator to convert it, but the result was not...

We had no way, no way, we had to write one by ourselves

The code can control the length of the digital code. For the processing part that is not suitable for the length, a function that automatically configures the length may be added back, but now it is still necessary to manually enter the length

There is not so much demand for improvement now;

EVERYTHING//

1. The appropriate length can be automatically determined according to the input number, or according to the selected length;

2. It can be solved by bit operation, but it is not intuitive enough

3. The handling of negative numbers is not perfect, which is closely related to 1 and is the key to handling the problem.

#include <bits/stdc++.h>
using namespace std;
string s;
void getorginalcode(int x,int len)
{
    for(int i=len;i>=0;i--)
    {
        if(x&(1<<i))
        {
            s.push_back('1');
        }
        else
        {
            s.push_back('0');
        }
    }
}
intmain()
{
    int n;
    while(~scanf("%d",&n))
    {
        int len;
        cout<<"Please input the code len you need;len = 4,8,16,32,etc...."<<endl;
        scanf("%d",&len);
        s.clear();
        cout<<"Please input the code you need : 原码(org),补码(fills),反码(rev)"<<endl;
        string opr;
        cin >> opr;
        if(opr=="org")
        {
            getorginalcode(n,len);
            cout<<s<<endl;
        }
        else if(opr=="fills")
        {
            getorginalcode(n,len);
            if(n>=0) cout<<s<<endl;
            else
            {
                for(int k=0;k<s.length();k++)
                {
                    if(s[k]-'0')
                    {
                        s [k] = '0';
                    }
                    else
                    {
                        s [k] = '1';
                    }
                }
                s[0]='1';//Sign bit
               int jw = 1;
               for(int j = s.length()-1;j>=0&&jw;j--)
               {
                   if(s[j]=='1')
                   {
                       s[j] = '0';
                       jw = 1;
                   }
                   else
                   {
                       s[j] = '1';
                       jw = 0;
                   }
               }
            }
        }
        else if(opr=="rev")
        {
            getorginalcode(n,len);
            if(n>=0)
            {
                cout<<s<<endl;
            }
            else
            {
                for(int k=0;k<s.length();k++)
                {
                    if(s[k]-'0')
                    {
                        s [k] = '0';
                    }
                    else
                    {
                        s [k] = '1';
                    }
                }
                s[0]='1';//Sign bit
                cout<<s<<endl;
            }
        }
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324781057&siteId=291194637