Saturday, April 11, 2015

Adding 2 Binary Numbers in C

//This function adds two binary numbers.
void  AddBinaryNumbers(int iarrNumber1[], int iNoOfDigitsInNumber1, int iarrNumber2[], int iNoOfDigitsInNumber2, int iarrSum[], int iNoOfDigitsInResult)
{
   //The loop counter
   int         iLoopCounter                        = 0;
   //The carry over digit
   int         iCarryOverDigit                     = 0;

   //Iterating through the bigger number
   for (; iLoopCounter < iNoOfDigitsInResult; iLoopCounter++)
   {
      //The sum of two digits
      int  iSumOfDigits = iarrNumber1[iLoopCounter] + iarrNumber2[iLoopCounter] + iCarryOverDigit;

      switch (iSumOfDigits)
      {
         case 0:  //The resultant bit is 0.

                  iarrSum[iLoopCounter] = 0;
                  break;

         case 1:  //The resultant bit is 1.

                  iarrSum[iLoopCounter] = 1;
                  break;

         case 2:  //The resultant bit is 0 and the carry over is 1.

                  iarrSum[iLoopCounter] = 0;
                  iCarryOverDigit = 1;
                  break;

         case 3:  //The resultant bit is 1 and the carry over is 1.

                  iarrSum[iLoopCounter] = 1;
                  iCarryOverDigit = 1;
                  break;
      }
   }

   //Computing the last digit if needed
   iarrSum[iNoOfDigitsInResult - 1] = iCarryOverDigit;
}

//The main function
void main()
{
   //The two arrays holding the two numbers
   int     *piarrNumber1                       = NULL;
   int     *piarrNumber2                       = NULL;
   //The array holding the sum of the two input numbers
   int     *piarrSum                           = NULL;
   //The size of the arrays
   int     iNoOfDigitsInNumber1                = 0;
   int     iNoOfDigitsInNumber2                = 0;
   int     iNoOfDigitsInResult                 = 0;
   //The loop counter
   int     iLoopCounter                        = 0;

   do
   {
      //Asking for the number of digits in the first number
      printf("\nEnter the number of digits in the first number.\n");
      scanf("%d", &iNoOfDigitsInNumber1);
      piarrNumber1 = (int *) calloc(iNoOfDigitsInNumber1, sizeof(int));

      //Asking for the user input
      printf("\nEnter the binary digits of the first number separated by space.\n\n");
      for (iLoopCounter = iNoOfDigitsInNumber1 - 1; iLoopCounter >= 0; iLoopCounter--)
         scanf("%d", &piarrNumber1[iLoopCounter]);

      //Verifying the input
      for (iLoopCounter = 0; iLoopCounter < iNoOfDigitsInNumber1; iLoopCounter++)
      {
         if (0 > piarrNumber1[iLoopCounter] || 1 < piarrNumber1[iLoopCounter])
         {
            printf("\nThe digit %d at index %d is not a binary digit.\n", piarrNumber1[iLoopCounter], iLoopCounter + 1);
            free(piarrNumber1);
            piarrNumber1 = NULL;
            break;
         }
      }
   }
   while (!piarrNumber1);

   do
   {
      //Asking for the number of digits in the second number
      printf("\nEnter the number of digits in the second number.\n");
      scanf("%d", &iNoOfDigitsInNumber2);
      piarrNumber2 = (int *) calloc(iNoOfDigitsInNumber2, sizeof(int));

      //Asking for the user input
      printf("\nEnter the binary digits of the second number separated by space.\n\n");
      for (iLoopCounter = iNoOfDigitsInNumber2 - 1; iLoopCounter >= 0; iLoopCounter--)
         scanf("%d", &piarrNumber2[iLoopCounter]);

      //Verifying the input
      for (iLoopCounter = 0; iLoopCounter < iNoOfDigitsInNumber2; iLoopCounter++)
      {
         if (0 > piarrNumber2[iLoopCounter] || 1 < piarrNumber2[iLoopCounter])
         {
            printf("\nThe digit %d at index %d is not a binary digit.\n", piarrNumber2[iLoopCounter], iLoopCounter + 1);
            free(piarrNumber2);
            piarrNumber2 = NULL;
            break;
         }
      }
   }
   while (!piarrNumber1);

   //Allocating sufficient space for the array
   if (iNoOfDigitsInNumber1 < iNoOfDigitsInNumber2)
      iNoOfDigitsInResult = iNoOfDigitsInNumber2 + 1;
   else
      iNoOfDigitsInResult = iNoOfDigitsInNumber1 + 1;
   piarrSum = (int *) calloc(iNoOfDigitsInResult, sizeof(int));

   //Adding the two numbers
   AddBinaryNumbers(piarrNumber1, iNoOfDigitsInNumber1, piarrNumber2, iNoOfDigitsInNumber2, piarrSum, iNoOfDigitsInResult);

   //Displaying the result
   for (iLoopCounter = 0; iLoopCounter < iNoOfDigitsInResult; iLoopCounter++)
      printf("\n%d", piarrSum[iLoopCounter]);
}

No comments:

Post a Comment