【发布时间】: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。