
#include <iostream>
#include <math.h>
#include <string> // We will store the binary version as a string.
#include <iomanip>

std::string Int_to_Binary(int );

int main(void)
{
   int a, b, c;

   std::cout << "Input two integers: ";
   std::cin >> a >> b;
   std::cout <<  "You entered: " << a << " and " << b << std::endl;

   std::cout << std::setw(14) << a << " = " << std::setw(8)
	     << Int_to_Binary(a) << std::endl;
   std::cout << std::setw(14) << b << " = " << std::setw(8)
	     << Int_to_Binary(b) << std::endl;

   c = a^b;
   std::cout << "XOR: a^b = " << std::setw(4) << c << " = "
	     << std::setw(7) << Int_to_Binary(c) << std::endl;
   c = a&b;
   std::cout << "AND: a&b = " << std::setw(4) << c << " = "
	     <<  std::setw(7) << Int_to_Binary(c) << std::endl;
   c = a|b;
   std::cout << " OR: a|b = " << std::setw(4) << c << " = "
	     << std::setw(7) << Int_to_Binary(c) << std::endl;

  return(0);
}


// Function: Int_To_Binary
// Argument: a single integer, a
// Returns: string containing binary
//   representation of a
std::string Int_to_Binary(int a)
{
   std::string A="";

   for (int i=(int)log2(a); i>=0; i--)
   {
      if ( a >= pow(2,i))
      {
	 A=A+"1";
	 a=a-pow(2,i);
      }
      else
	 A=A+"0";
   }
   return(A);
}
