This code is a failure and will not work, but enjoy trying to get it to work, as I cannot...
Code:
#include <stdio.h>
#include <stdlib.h>
// Allow boolean types
typedef enum
{
false,
true
} bool;
// Add binary numbers and return binary
int addBinary (int b1, int b2);
int main (void)
{
// Binary numbers b1 and b2 get added together
int b1 = 111, b2 = 101;
// Binary addition answer
int b3;
// Add binary numbers
b3 = addBinary (b1, b2);
// Print out binary addition
printf ("111 + 101 = %d\n", b3);
return 0;
}
// Add binary numbers and return binary
int addBinary (
int b1, // The first binary number
int b2) // The second binary number
{
// The binary addition answer, transforms into binary.
int a;
// Carrier for binary addition
bool carrier = false;
// Current digit step (starts at 1)
int s = 2;
// Add the numbers like decimal
a = b1 + b2;
// Next Binary digit
NBD:
// If the carrier is true, add one to the current placement
if (carrier)
{
a += 1 * (s - (s / 2));
carrier = false;
}
// If the carrier makes the postdigit a 3, we have trouble
if (a % (s / 2 + s) < s / 2)
{
a += 1 * ((s * 10) - ((s * 10) / 2));
a -= 1 * (s - (s / 2));
}
// If the addition yields a 2 * digit placement,
// remove it and set carrier binary addition to true.
if (a % s < (s / 2))
{
a -= 1 * (s - (s / 2));
carrier = true;
}
// If our digit placement is high enough, exit
if (s > a)
return a;
// Set the digit placement higher
s *= 10;
// Verify next placement (Next binary Digit)
goto NBD;
}