#include <stdio.h>
#include <iomanip>
int main()
{
int binary1[8] = {0,1,1,1,1,0,1,1}; //8 element array
int binary2[8] = {1,0,1,0,1,1,1,1}; //8 element array
int binarySum[9];
int overflow [9];
int i;
for (i=0; i<9; i++) overflow[i] = 0;
for (i=9; i >= 0; i--)
{
binarySum[i] = binary1[i] + binary2[i] + overflow[i];
if ( binarySum[i] > 1)
{
overflow[i-1] = 1;
binarySum[i] %= 2;
}
}
printf("Binary Sum is: ");
for (i = 0; i<9; i++) printf("%i", binarySum[i]);
printf("\n");
printf("\n");
printf("Carry Bit is: ");
for (i = 8 ; i>=0 ; i--) printf("%i", overflow[i]);
printf("\n");
return (0);
}
my output is:
Binary Sum is: 00101010-1717986920
Carry Bit is: 001111111
-I'm getting debug error message.
-why my 9th bit is long negative number on binary sum?
I should get "100101010".
-carry bits should be "011111111" but I'm getting "001111111".
any help~~