plz help me with code c++ for binary full adder
that sum to binaryn like
1111
+ 0110
#include <iostream>
using std::cout;
using std::endl;
const int LENGTH = 5;
char sumChars(char x, char y, char& carry);
int main()
{
char first[LENGTH + 1] = "10100";// 20
char second[LENGTH + 1] = "01110";// 14
char result[LENGTH + 1] = { 0 };
char carry = '0';
cout << first << " + " << second << " = ";
for (int i = LENGTH - 1; i >= 0; --i)
{
result = SumChars(first, second, carry);
if (carry == '1')
{
if (i == 0)
break;
if (first[i - 1] == '0')
{
first[i - 1] = '1';
carry = '0';
} else
{
first[i - 1] = '0';
carry = '1';
}
}
}
cout << result << "\n,carry = " << carry << endl;
return 0;
}
char sumChars(char x, char y, char& carry)
{
char res = '0';
if (x == '0' && y == '0')
{
res = '0';
carry = (carry == '1') ? '1' : '0';
} else if (x == '0' && y == '1')
{
res = '1';
carry = (carry == '1') ? '1' : '0';
} else if (x == '1' && y == '0')
{
res = '1';
carry = (carry == '1') ? '1' : '0';
} else if (x == '1' && y == '1')
{
res = '0';
carry = '1';
}
cout << x << " and " << y << " = " << res << " ,carry = " << carry << endl;
return res;