/* This program calculates -- via HalfAdd() -- the outputs from a
   boolean expression that is equivalent to the logical circuit for
   a binary half-adder.

   Input:  Two binary digits

   Output: Two binary values representing the sum and
           carry that result when the input values are added
------------------------------------------------------------------*/

#include <iostream.h>

void HalfAdd(int, int, int&, int&);

int main(void)
{
   cout << "\nThis program simulates the execution of "
        << "a binary half-adder.\n";

   int
      ABit,                 // the two binary inputs
      BBit,
      SumBit,               // the sum bit
      CarryBit;             // the carry bit
   char
      Answer;               // for the user's query response

   do
   {
      cout << "\nPlease enter two binary inputs: ";
      cin >> ABit >> BBit;

      HalfAdd(ABit, BBit, SumBit, CarryBit);

      cout << "\n\tCarry = " << CarryBit
           << " Sum = " << SumBit << '\n';

      cout << "\nDo you have more additions to perform (y or n) ? ";
      cin >> Answer;
   }
   while (Answer != 'n');

   return 0;
}

/* This function implements the binary half-adder operation.

   Receive: Two binary inputs A and B
   Return:  The Sum and Carry bits of A + B
---------------------------------------------------------------*/

void HalfAdd(int A, int B, int& Sum, int& Carry)
{
   Sum = (A || B) && !(A && B);
   Carry = (A && B);
}

