L
LaVic
Hello Everyone,
I am trying to add two binary numbers in the form of two arrays. The
code that i have been using compiles, but it does not give me the
results i would expect. The problem that i am having is that it does
not carry ones over to the next significant digit if i have a 1 + 1 =
10 case. Could anyone help me by giving an explanation in the form of a
correction if possible. I would appreciate any feedback. Thank You !
Here is the code
// Binary number manipulation
#include
#include
#include
//#include
main()
{
int array_value1[ 4 ] = { 0, 0, 1, 1 }; //4 element array
int array_value2[ 4 ] = { 0, 1, 0, 1 }; //4 element array
int array_value3[4];
int i;
for( i = 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value1 );
for( i= 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value2);
for (i=0; i <4; i++)
{
array_value3 = array_value1 + array_value2;
if ( (array_value1== 1) && (array_value2== 1))
{
array_value3 = 0;
array_value3[i+1] = 1 + array_value1[i+1] + array_value2[i+1];
}
}
for( i= 0; i <4; ++i)
printf("d[%i] = %i \n", i, array_value3);
return(0);
}
I am trying to add two binary numbers in the form of two arrays. The
code that i have been using compiles, but it does not give me the
results i would expect. The problem that i am having is that it does
not carry ones over to the next significant digit if i have a 1 + 1 =
10 case. Could anyone help me by giving an explanation in the form of a
correction if possible. I would appreciate any feedback. Thank You !
Here is the code
// Binary number manipulation
#include
#include
#include
//#include
main()
{
int array_value1[ 4 ] = { 0, 0, 1, 1 }; //4 element array
int array_value2[ 4 ] = { 0, 1, 0, 1 }; //4 element array
int array_value3[4];
int i;
for( i = 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value1 );
for( i= 0; i <4; i++)
// printf("d[%i] = %i \n", i, array_value2);
for (i=0; i <4; i++)
{
array_value3 = array_value1 + array_value2;
if ( (array_value1== 1) && (array_value2== 1))
{
array_value3 = 0;
array_value3[i+1] = 1 + array_value1[i+1] + array_value2[i+1];
}
}
for( i= 0; i <4; ++i)
printf("d[%i] = %i \n", i, array_value3);
return(0);
}