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;

Recommended Answers

All 5 Replies

What exactly do you need help with?

sum of binary number
1010
+0101
=1111

also we have situation that has carry
1111
1111
=
like this

1111
+1111
=11110

yes my code do this
but it doesn't out the true ansewr

I understand that everything in your code seems to be working write.
you must be probably getting an output like this
1111+1111 = 1110 , carry=1
right?

The problem with your program is that, its considering that 4 digits are the maximum space required for the result. (if a left-trailer of 0 is added your problem will be solved.)

actually during addition, you may want to consider something like this.
01111
+01111
-------
Then your program will be giving the right output..
This is because your carry is supposed to be added to the next SumChar() sequence.
As there is no digits , the program must assume 0's and then carry on the computation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.