I'm currently doing a college task where I have to add two binary numbers in C (without arrays). I understand the algorithm, I mean, how to add binary numbers, but I don't get how to write it as code. In this specific exercise the user has to enter two digits (0 or 1) per Operand. a1 and a2 for the first Operand and b1 and b2 for the second one. For example:
User introduce 1 1 (a1, a2) for the first binary number and 0 1 (b1, b2) for the second. The result (100) is what the programm have to return back. I have already seen some code examples, but still don't understand how it works. Using if and else if statements I can understand a part of it (if I'm not wrong):
if((a2 == 0) && (b2 == 0)) {
sum = 0;
carry = 0;
}
else if((a2 == 0) && (b2 == 1) || (a2 == 1) && (b2 == 0)) {
sum = 1;
carry = 0;
}
else if((a2 == 1) && (b2 == 1)) {
sum = 0;
carry = 1;
{
But how do I continue? I want to make clear that I don't want the code (I can copy it from Internet if I want to), I want to understand it.