C语言中的2次方补码介绍

340 阅读4分钟

2s Complement in C

C语言中的2次方补码介绍

在进入2的补码时,我们首先要了解什么是1的补码。任何二进制数的1的补码,通过将所有的比特改变成等效的补码形式,就可以得到另一个二进制数,比如0会被转化为1,1会被转化为0。同样,2的补码的定义是1加到1的补码上,然后结果成为2的补码。在本专题中,我们将学习C语言中的2s补码。

实时使用。在 日常生活中,我们观察到许多有准确时间间隔的彩色闪烁的灯。在这种情况下,我们使用了1和2的补码。为什么?因为每个1代表开启,每个0代表关闭。最初的二进制数字给出了一个 "开 "和 "关 "的模式,而1和2的补码给出了完全相反的 "开 "和 "关 "的动作模式。这就导致了我们所有的灯都在闪烁。

优点。

  • 在安全编码中很有用。
  • 有助于节日期间连续闪烁的灯光。

2的补码在C语言中是如何工作的?

实现2的补码的逻辑。

  1. 向用户询问输入的数字,并将其存储在任何变量中,假设 "输入"。
  2. 然后找出输入数字的1的补数。
  3. 考虑2个变量,如2个补数和carryDigit=1。
  4. 从0到数字的实际大小进行循环迭代。
  5. 主要是我们在for a循环内有3种情况。
  • 如果2个变量1的补码和carryDigit都是1,那么我们可以在2的补码中插入0。
  • 如果1的补码是0,carryDigit位是1,那么,我们可以在2的补码中插入1,并将carryDigit值加到0。
  • 如果carryDigit是0,那么就把1的补码的值分配给2的补码。

语法。

for(int p = length - 1; p >= 0; p--)
{
ifoneComplement[p] == '1' && carryDigit == 1)
{
oneComplement[p] = '0';
}
else if(oneComplement[p] == '0' && carryDigit == 1)
{
twoComplement[p] = '1';
carryDigit = 0;
}
else
{
twoComplement[p] = oneComplement[p];
}
}

C语言中2s补码的例子

下面是C语言中2s补码的例子。

例子 #1

8位二进制数的2次方补码

代码。

#include<stdlib.h>
#define LENGTH 8
//main method for executing the code
int main()
{
//initialize carry variable
int carryDigit = 1;
//initialoze 1's and 2's complement variables
char input[LENGTH + 1], oneComplement[LENGTH + 1], twoComplement[LENGTH + 1];
//Ask user to enter 8 digit binary number
printf("Please enter 8 digit binary number=>\n");
gets(input);//equivalent to scanf method
//1's complement logic within for loop
for( int var= 0; var < LENGTH; var++)
{
if(input[var] == '0')
{
oneComplement[var] = '1';
}
else if(input[var] == '1')
{
oneComplement[var] = '0';
}
}
oneComplement[LENGTH] = '\0';
//2's complement logic within for loop
for(int var = LENGTH - 1; var >= 0; var--)
{
if(oneComplement[var] == '1' && carryDigit == 1)
{
twoComplement[var] = '0';
}
else if(oneComplement[var] == '0' && carryDigit == 1)
{
twoComplement[var] = '1';
carryDigit = 0;
}
else
{
twoComplement[var] = oneComplement[var];//say 1's and 2's complement are same
}
}
twoComplement[LENGTH] = '\0';
//printing 2's complement output
printf("2's complement of 8 digit binary number %s is=> %s\n",input, twoComplement);
return 0;
}

输出。

2s complement in c output 1

例子 #2

另一种方法是使用2's complement的方法,输入范围可以达到16位数

代码:

#include<stdio.h>//PRovide basic C libraries
#include<conio.h>
#include<string.h>//Provide String library for String operations
#define LENGTH 16// declaring constant for using in the entire application
main()
{
//declaring integer variables
int i,test;
//declaring character array variables
char binaryArray[LENGTH],tempArray[LENGTH],a[LENGTH];
//declaring userInput method
void userInput(char binaryArray[]);
//declaring validate method
int validate(char binaryArray[]);
//declaring twosComplement method
void twosComplement(char binaryArray[],char a[]);
//calling userInput method
userInput(binaryArray);
//copying one array to other array
strcpy(tempArray,binaryArray);
//calling validate method
test=validate(binaryArray);
//checking wheter given value is valid or not from user
if(test==0)
{
printf("\nPlease enter valid binary number");
exit(1);
}
//calling twosComplement method
twosComplement(binaryArray,a);
printf("\n2's complement is %s",a);
getch();
}
//logic for userInput method for asking user user input
void userInput(char binaryArray[])
{
printf("Please enter the binary number maximum length of 16 \n");
scanf("%s",binaryArray);
return;
}
//validate the user input
int validate(char binaryArray[])
{
int i,l,x=1;
l=strlen(binaryArray);
for(i=0; i<l; i++)
{
if(!((binaryArray[i]=='0')||(binaryArray[i]=='1')))
x=0;
break;
}
return(x);
}
//finding the 2's complement logic
void twosComplement(char binaryArray[],char a[])
{
int validate;
/*char a[LENGTH];*/
int l,i;
l=strlen(binaryArray);
for(i=l-1; i>=0; i--)
{
if(binaryArray[i]=='0')
a[i]='1';
else
a[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if(a[i]=='0')
a[i]='1';
else
{
a[i]='0';
validate=1;
}
}
else
{
if((validate==1)&&(a[i]=='0'))
{
a[i]='1';
validate=0;
}
else if((validate==1)&&(a[i]=='1'))
{
a[i]='0';
validate=1;
}
}
}
a[l]='\0';
return;
}

输出。

2s complement in c output 2

例子 #3

十进制数字2的补码

代码:

#include<stdio.h>
#include<stdlib.h>
#define LENGTH 8
//main method for executing the code
int main()
{
//initialize carry variable
int carryDigit = 1;
//initialoze 1's and 2's complement variables
char input[LENGTH + 1], oneComplement[LENGTH + 1], twoComplement[LENGTH + 1];
//declaring integer variables
int a[10],n,i;
//ask the user to enter any decimal number
printf("Enter the number to convert: ");
scanf("%d",&n);
int tempInput=n;
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("Binary number of the input is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
input[i] = a[i] + '0';
}
printf("\n");
for( int var= 0; var < LENGTH; var++)
{
if(input[var] == '0')
{
oneComplement[var] = '1';
}
else if(input[var] == '1')
{
oneComplement[var] = '0';
}
}
oneComplement[LENGTH] = '\0';
//2's complement logic within for loop
for(int var = LENGTH - 1; var >= 0; var--)
{
if(oneComplement[var] == '1' && carryDigit == 1)
{
twoComplement[var] = '0';
}
else if(oneComplement[var] == '0' && carryDigit == 1)
{
twoComplement[var] = '1';
carryDigit = 0;
}
else
{
twoComplement[var] = oneComplement[var];//say 1's and 2's complement are same
}
}
twoComplement[LENGTH] = '\0';
//printing 2's complement output
printf("2's complement of 8 digit decimal number %d is=> %s\n",tempInput, twoComplement);//Ignore @ in the output at last digit
return 0;
}

输出。

output 3

结论

C语言的2s补码用于闪灯和安全编码的应用。所以我们只需在1的补码输出中加上1,就可以得到2s补码的结果输出。