/**
* 十进制转化为2进制原码
*
* @param num 用户输入的十进制
*/
void tenZhuanTwoYuanMa(int num)
{
int num1[32];
if (num>0)
{
num1[0] = 0;
}
else
{
num1[0] = 1;
num*=(-1);
}
for (int i = 0; i <31; i++)
{
num1[31-i] = num%2;
num/=2;
}
for (int i = 1; i<=32; i++)
{
printf("%d",num1[i-1]);
if (i%8 == 0)
{
printf(" ");
}
}
}
/**
* 用户输入的十进制转化为二进制反码
*
* @param num 用户输入的十进制
*/
void tenZhuanTwoFanMa(int num)
{
int num1[32];
if (num>=0)
{
num1[0] = 0;
for (int i = 0; i <31; i++)
{
num1[31-i] = num%2;
num/=2;
}
}
else
{
num1[0] = 1;
num*=(-1);
for (int i = 0; i <31; i++)
{
num1[31-i] = num%2;
if(num1[31- i]==0)
{
num1[31-i] = 1;
}
else
{
num1[31 - i] = 0;
}
num/=2;
}
}
for (int i = 1; i<=32; i++)
{
printf("%d",num1[i-1]);
if (i%8 == 0)
{
printf(" ");
}
}
}
/**
* 10进制转化为2进制的补码
*
* @param num 用户输入的值
*/
void tenZhuanTwoBuMa(int num)
{
int num1[32];
if (num>=0)
{
num1[0] = 0;
for (int i = 0; i <31; i++)
{
num1[31-i] = num%2;
num/=2;
}
}
else
{
num1[0] = 1;
num*=(-1);
for (int i = 0; i <31; i++)
{
num1[31-i] = num%2;
if(num1[31- i]==0)
{
num1[31-i] = 1;
}
else
{
num1[31 - i] = 0;
}
num/=2;
}
for (int k = 0; k < 31; k++)
{
if (*(num1+31-k)== 0)
{
*(num1+31-k)= 1;
for (int j = 0; j < k; j++)
{
*(num1+31-j) = 0;
}
break;
}
}
}
for (int i = 1; i<=32; i++)
{
printf("%d",num1[i-1]);
if (i%8 == 0)
{
printf(" ");
}
}
}
|
|