created NOT,AND,OR and XOR functions(using NAND) then created the 'adder_prim' and 'adder_carry' functions for the primary output and the carry. Used the standard full adder circuit and put it in a loop(10 cycles so should be able to add til 2031).
entered the input in the code itself(X is 139,Y is 74) just to see if it's working properly or not.
instead of 216(correct ans) it's coming 196 and i have no clue why.
int NAND(int i,int j)
{
int A;
A=((i==1)&&(j==1))?0:1;
return A;
}
int NOT(int i)
{
int A=NAND(i,i);
return A;
}
int AND(int i,int j)
{
int A=NOT(NAND(i,j));
return A;
}
int OR(int i,int j)
{
int A=NAND((NAND(i,i)),NAND(j,j));
return A;
}
int XOR(int i,int j)
{
int A=OR(AND(i,NOT(j)),AND(NOT(i),j));
return A;
}
int adder_prim(int,int,int);
int adder_carry(int,int,int);
int _tmain(int argc, _TCHAR* argv[])
{
int Z[10];
int C=0;
int X[]={0,0,1,0,0,0,1,0,1,1};
int Y[]={0,0,0,1,0,0,1,0,1,0};
for(int i=0;i<10;i++)
{
Z[i]=adder_prim(X[i],Y[i],C);
C=adder_carry(X[i],Y[i],C);
}
for(int j=0;j<10;j++)
{
cout <<Z[j];
}
getch();
return 0;
}
int adder_prim(int a,int b,int c)
{
int O=XOR(XOR(a,b),c);
return O;
}
int adder_carry(int a,int b,int c)
{
int C=OR(AND(XOR(a,b),c),AND(a,b));
return C;
}
You need to add the least significant bit first, not the most significant bit:
for(int i=9;i>-1;i--)
{
Z[i]=adder_prim(X[i],Y[i],C);
C=adder_carry(X[i],Y[i],C);
}