【问题标题】:regarding MSB, in representing negative decimal to binary equivalent in c++关于 MSB,在 c++ 中将负十进制表示为二进制等效项
【发布时间】:2022-09-26 02:42:15
【问题描述】:

我试图表示负十进制 NO。成二进制

并且我的代码按设计工作,但我不确定我的设计是否实现了我的目标。\"

但我对 MSB 有疑问

我知道对于负十进制数,MSB 以二进制形式保持为 1

所以我为表示一个数字的 2\'s 补码做了什么,我保持 MSB 0,因为目前我正在显示 2\'s 正数的补码

但是为了将相同的 2\'s 补码表示为负十进制数的二进制表示

我将 MSB 更改为 1

这是输出:

binary representation of 8 is 1000
full binary representation of 8 is 00000000000000000000000000001000
1\'s complement of  8 is 11111111111111111111111111110111
2\'s complement of  8 is 01111111111111111111111111111000

as negative decimal numbers are basically stored as 2\'s complement of postive decimal equivalent 
Full  binary representation of -8 is 11111111111111111111111111111000

...Program finished with exit code 0
Press ENTER to exit console.

我想知道打印2的正数补码是否等于负数的二进制表示? 所以我的方法是对还是错??? 如果需要,这里是代码

//!!!!this code is not working in my vscode but woeking on online compilers
#include<iostream>
#include<cmath>
using namespace std;

void DeciToBin(int a)
{
    int b=a;
    int sum=0;
    int bit,q=0;

    if(a>0)                                            
    {                                                    
        //binary of 12 is   1100                        
        while(a!=0)
        {
            bit= a&1;

            sum= ( bit * pow(10,q) ) + sum;
            //cout<<pow(10,q)*bit<<endl;
            
        // cout<<sum<<endl;
            a= a >>1;
            //cout<<a<<endl;
            q++;
        }
    }
   
    else
    {
       
       int e=-a;
       int arr[32];  //since integer cant store 32 bit length to representation binary we will operate on array
       int count=0;

       while(e!=0)
        {
            bit= e&1;

            sum= ( bit * pow(10,q) ) + sum;
            //cout<<pow(10,q)*bit<<endl;
            count++;
            e= e >>1;
            q++;
        }
        
       cout<<\"binary representation of \"<< (-a) <<\" is \"<<sum<<endl;  
       
       
        int sum2=sum;
        for(int i=0;i<32;i++)
        {
            arr[i]=0;
        }
        
       for (int i = 31; i >=32-count; i--)    //storing everybit in array (normal representation of decimal number in binay)
       {
            arr[i]= sum2%10;
            sum2=sum2/10;
       }
       
       
       cout<<\"full binary representation of \"<< (-a) <<\" is \";  
       for(int i=0;i<32;i++)         //output if decimal number is 6 :  00000000000000000000000000000110 
            cout<<arr[i];
       
       
      //1\'s complement
      for(int i=0;i<32;i++)
      {
          if(arr[i]==1)
                arr[i]=0;
          else
                arr[i]=1;
      }
      
       cout<<\"\\n1\'s complement of  \"<< (-a) <<\" is \";  
       for(int i=0;i<32;i++)        
            cout<<arr[i];
        
        
        /* convert back to its original binary form, so that we can apply trick to calculate 2\'s complement
        which works directly on original bibary form of a decimal number */
        for(int i=0;i<32;i++)
        {
          if(arr[i]==1)
                arr[i]=0;
          else
                arr[i]=1;
        }
   
            
        //2\'s complement by using trick on GFG ** this trick works directly on binary of number not on 1s complement
        for( int i=31 ; i>0 ; i-- )
        {
            if(arr[i]==1)   //check from LSB if the bit is 1 or not , if 1 then turn rest bits in 1(if 0) or in 0(if 1)
            {               // ex  number is 0110100 then it will 1001000 is a 2\'s complement
            
                for(int j = i-1 ; j>0 ; j--)    // keep j>0 if number is positve and j>=0 if number is negative
                {                                   // as MSB defines if number is negative or +ve ,its for representation only
                    if( arr[j] == 0 )
                        arr[j] = 1;
                }
            break;
            }   
        }
    
        cout<<\"\\n2\'s complement of  \"<< (-a) <<\" is \";  
        for(int i=0 ; i<32 ; i++ )        
            cout<<arr[i];
            
        
        cout<<endl<<endl<<\"as negative decimal numbers are basically stored as 2\'s complement of postive decimal equivalent \";    
        
        arr[0]=1;   //since number is negative i am changing MSB to 1
        cout<<endl<<\"Full  binary representation of \"<<a<<\" is \";
        for(int i=0;i<32;i++)        
            cout<<arr[i];
        
    }   //end of else
    //cout<<\"binary Form of \"<<b << \" is \"<<sum<<endl;
}
int main()
{   
    //system(\"cls\");
    int a=-8;
    DeciToBin(a);
    return 0;
}
  • 当从 1s 补码变为 2s 补码时,您如何/为什么将 MSB 从 1 更改为 0
  • 无关,但请不要将浮点 pow 用于整数幂。我建议您创建自己的函数来处理整数幂。
  • @AdrianMole 实际上我没有从 1 的补码计算 2 的补码我使用技巧直接从原始二进制形式计算 2 的补码
  • @Someprogrammerdude 是的,很多人建议我不要使用 pow 我打算稍后编写自己的幂函数
  • 你的 8 的补码是错误的,MSB 应该是1

标签: c++ binary decimal


【解决方案1】:

即使忽略输出 2 的补码表示不正确的事实,程序的输出也不正确:

8 在 1 或 2 的补码中的表示与原始二进制文件匹配。您正在尝试在此处打印否定表示,即表示 -8 的位序列。

此外,我不知道你从哪里得到否定 2 的补数的技巧,但它要么错误,要么实现不正确。

这是我认为更简单的方法:

  1. 交换所有位。
  2. 将位序列视为无符号整数,然后将结果值模 2^n 加 1,即在获得与原始位数相同的位数后简单地删除进位。
    {
        // Negating a number in 2's complement (which the original value is in) can be done by negating every bit and then
        // adding 1 as if the number was unsigned and ignoring overflow
    
        int carry = 1; // we're swapping bits and adding 1 at the same time
        for (size_t i = 32; i != 0;)
        {
            --i; // delay the decrement, since the loop variable is unsigned
            auto val = (1 - arr[i]) + carry;
            if (val == 2)
            {
                val = 0;
                carry = 1;
            }
            else
            {
                carry = 0;
            }
            arr[i] = val;
        }
    }
    

    请注意,标准库中有一些功能可以大大简化这一点。

    #include <bit>
    #include <bitset>
    #include <cstdint>
    #include <iomanip>
    #include <iostream>
    
    template<class T>
    void PrintBinary(int32_t valueDecimal, T value, char const* representation)
    {
        std::cout << std::setw(2) << valueDecimal << " is " << std::bitset<32>(std::bit_cast<uint32_t>(value)) << " in " << representation << '\n';
    }
    
    int main()
    {
        constexpr uint32_t value = 8;
        constexpr int32_t negatedValue = -value;
    
        PrintBinary(value, value, "binary");
        PrintBinary(negatedValue, ~value, "1's complement representation");
        PrintBinary(negatedValue, negatedValue, "2's complement representation");
    }
    

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 2019-04-26
    • 2021-05-16
    • 2013-11-07
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2012-10-30
    • 2014-04-17
    相关资源
    最近更新 更多