2's complement of binary number
There are many different ways to find the 2's complement of the binary number. One of the way is start from LSB and till the '1' comes for the first time copy the bits as it is till the first 1 place and then flip rest of the bits.
Implementation in C:
#include<stdio.h>
#include<string.h>
void complement(char*);
int main()
{
char a[] = "1010100";
complement(a);
return 0;
}
void complement(char *a)
{
int l = strlen(a)-1;
while (a[l] == '0')
{
l--;
}
l--;
while (l>=0)
{
if(a[l] == '0')
a[l] = '1';
else
a[l] = '0';
l--;
}
printf("2's Complement of binary number: %s",a);
}
Algorithm to find 2's Complement (Outplace)Implementation in C:
#include<stdio.h>
#include<string.h>
void complement(char*);
int main()
{
char a[] = "1010100";
complement(a);
return 0;
}
void complement(char *a)
{
int l = strlen(a)-1;
while (a[l] == '0')
{
l--;
}
l--;
while (l>=0)
{
if(a[l] == '0')
a[l] = '1';
else
a[l] = '0';
l--;
}
printf("2's Complement of binary number: %s",a);
}
Comments
Post a Comment