Hello, i am trying to figure out what i did wrong. i am trying to add a subtraction flag to my current 32 bit adder, and i keep getting the incorrect output back.
For example, if the user enters:
input 32 bits: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
input 32 bits: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
subtraction flag:0
this should be the answer:
answer:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
but i am instead getting:
answer:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
It seems the carry isn't carrying properly. I was told to set the CarryIn to the subtraction flag, which I did, but it seems to have overwritten it somehow. Any help would be greatly appreciated. Thanks for your time, code is posted below.
#include <stdio.h>
#define SIZE 32
int a[SIZE];
int b[SIZE];
int sum[SIZE];
int sflag(int a, int b, int c)
{
return (a && !b && c);
}
int carry(int a, int b, int c)
{
return (a && b) || (b && c) || (a && c);
}
int add(int a, int b, int c)
{
return (a && !b && !c) || (!a && b && !c) || (!a && !b && c) || (a && b && c)\
;
}
int main()
{
int i;
int CarryIn;
int flag;
printf("input 32 bits:");
for(i=SIZE-1;i>=0;i--)
scanf("%d",&(a[i]));
printf("input 32 bits:");
for(i=SIZE-1;i>=0;i--)
scanf("%d",&(b[i]));
printf("subtraction flag:");
scanf("%d", &flag);
CarryIn = flag;
for(i=0;i < SIZE;i++)
{
flag = sflag(a[i],b[i],flag);
sum[i] = add(a[i],flag,CarryIn);
CarryIn = carry(a[i],flag,CarryIn);
}
for(i=SIZE-1;i>=0;i--)
printf("%d ",sum[i]);
printf("\n");
return 0;
}