Saturday, May 19, 2012

Implementing 1's Complement & 2's Complement in C

1's complement and 2's complement arithmetic is used in Computer Science for implementing the basic operations of addition and subtraction. In this program , I have endeavored to provide a simple menu based example in C of these basic operations. 

To implement these operations I have developed the following functions:-

void printMenu()
{
clrscr();
gotoxy(5,5);
printf("Read\tPrint\t1'sComplement\t2'sComplement\tAdd\tSub\teXit\n");
printf("A = ");
printNumber(a);
printf("  B = ");
printNumber(b);
printf("  C = ");
printNumber(c);
printf("\n");
}
This is the Menu. It works by pressing R,P,1,2,A,S,X
The menu is not case sensitive.

void copyArray(int dest[],int src[])
{
 int i;
 for(i=0;i<=8;i++)
 dest[i]=src[i];
}
copies the src array into dest.


int getCarry(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(0);
 case 2:return (1);
 default:return(1);
}
}
adds 3 bits and returns the carry.



int getSum(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(1);
 case 2:return (0);
 default:return(1);
}
}
Adds three bits and returns the sum


void TwosComplement(int x[])
{
int temp[]={1,0,0,0,0,0,0,0,0};
int out[9];
copyArray(out,x);
OnesComplement(out);
addNumber(out,temp,x);
}
finds the 2's complement of a number and stores it in the same array.


void OnesComplement(int x[])
{
int i;
for(i=8;i>=0;i--)
x[i]=1-x[i];
}
finds the 1's complement of a number.

void printNumber(int x[])
{
int i;
for(i=8;i>=0;i--)
printf("%d",x[i]);
}
prints the number

void readNumber(int x[])
{
int i;
char str[10];
scanf("%s",str);
for(i=8;i>=0;i--)
x[i]=(str[8-i]-'0'); //0,1,2,3,4 '0'-'0'=0,'1'-'0'=1
 }

reads a number as a string of 9 characters and stores it in a int array.

void subNumber(int x[],int y[],int sub[])
{
int temp[9];
copyArray(temp,y);
TwosComplement(temp);
addNumber(x,temp,sub);
}
subtracts 2 numbers and stores it in the 3rd parameter

Important

Each array is 9 elements, 8 for the byte and the ninth for the sine bit.
Therefore values must be entered as such

Here are some snapshots:-









The Complete Program

Binary.c

#include<stdio.h>                                                  
#include<conio.h>
#include<math.h>
int a[9]={0,0,0,0,0,0,0,0,0},b[9]={0,0,0,0,0,0,0,0,0},c[9]={0,0,0,0,0,0,0,0,0};
void readNumber(int x[]);
void printNumber(int x[]);
void OnesComplement(int x[]);
void addNumber(int x[],int y[],int sum[]);
void TwosComplement(int x[]);
int getSum(int a,int b,int c);
int getCarry(int a,int b,int c);
void copyArray(int dest[],int src[]);
void subNumber(int x[],int y[],int sub[]);
void printMenu();
void main()
{

int option,ch;
clrscr();
while(1)
{
printMenu();
option=getch();
switch(option)
{
case'X':
case'x':
return;
case 'r':
case 'R':
printf("\nEnter a or b: ");
ch=getche();
printf(" = ");
if(ch=='a' || ch=='A')
readNumber(a);
else
readNumber(b);
break;
case '1':
printf("\nEnter a or b: ");
ch=getche();
printf(" = )");
if(ch=='a' || ch=='A')
OnesComplement(a);
else
OnesComplement(b);
break;

case '2':
printf("\nEnter a or b: ");
ch=getche();
printf(" = ");
if(ch=='a' || ch=='A')
TwosComplement(a);
else
TwosComplement(b);
break;
case 'a':
case 'A':
addNumber(a,b,c);
break;
case 's':
case 'S':
subNumber(a,b,c);
break;
}
}
}
void printMenu()
{
clrscr();
gotoxy(5,5);
printf("Read\tPrint\t1'sComplement\t2'sComplement\tAdd\tSub\teXit\n");
printf("A = ");
printNumber(a);
printf("  B = ");
printNumber(b);
printf("  C = ");
printNumber(c);
printf("\n");
}
void copyArray(int dest[],int src[])
{
 int i;
 for(i=0;i<=8;i++)
 dest[i]=src[i];
}
void addNumber(int x[],int y[],int sum[])
{
 int i,s,carry=0;
 for(i=0;i<=8;i++)
{
   s=getSum(x[i] ,y[i],carry);
   carry=getCarry(x[i],y[i],carry);
   sum[i]=s;
}
}
int getCarry(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(0);
 case 2:return (1);
 default:return(1);
}
}
int getSum(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(1);
 case 2:return (0);
 default:return(1);
}
}
void TwosComplement(int x[])
{
int temp[]={1,0,0,0,0,0,0,0,0};
int out[9];
copyArray(out,x);
OnesComplement(out);
addNumber(out,temp,x);
}
void OnesComplement(int x[])
{
int i;
for(i=8;i>=0;i--)
x[i]=1-x[i];
}
void printNumber(int x[])
{
int i;
for(i=8;i>=0;i--)
printf("%d",x[i]);
}

void readNumber(int x[])
{
int i;
char str[10];
scanf("%s",str);
for(i=8;i>=0;i--)
x[i]=(str[8-i]-'0'); //0,1,2,3,4 '0'-'0'=0,'1'-'0'=1
 }
void subNumber(int x[],int y[],int sub[])
{
int temp[9];
copyArray(temp,y);
TwosComplement(temp);
addNumber(x,temp,sub);
}
Next, we shall use it to implement the Booth's algorithm

No comments:

Post a Comment