This blog is under construction

Tuesday, 9 July 2013

C program to perform binary addition, subtraction and multiplication

If we know the procedure to perform binary addition, then we can do any binary arithmetic operations so easily.  Please check the below link to know how binary addition is performed.

Write a C program to perform binary addition, subtraction and multiplication.


  #include <stdio.h>
  #include <stdlib.h>

  /* performs binary addition for the given values */
  int binaryAddition(int n1, int n2) {
        int carry;
        while (n2 != 0) {
                /* calculating the carry and do a left shift*/
                carry = (n1 & n2) << 1;
                /* calculating the sum */
                n1 = n1 ^ n2;
                n2 = carry;
        }
        return n1;
  }

  /* performs binary subtraction for the given values */
  int binarySubtracton(int n1, int n2) {
        int carry;
        /* finding two's complement for n2 and add the o/p with n1 */
        n2 = binaryAddition(~n2, 1);

        while (n2 != 0) {
                /* calculating the carry and do a left shift*/
                carry = (n1 & n2) << 1;
                /* calculating the sum */
                n1 = n1 ^ n2;
                n2 = carry;
        }
        return n1;
  }

  /* performs binary multiplication for the given values */
  int binaryMultiplication(int n1, int n2) {
        int i, res = 0;
        for (i = 0; i < n2; i++) {
                res = binaryAddition(res, n1);
        }
        return res;
  }

  /* performs binary division for the given values */
  int binaryDivision(int n1, int n2) {
        int i, res, count = 0, twosComplement;
        if (n1 < n2) {
                printf("Division of %d and %d is %d\n", n1, n2, 0);
                return 0;
        }

        res = n1;
        twosComplement = binaryAddition(~n2, 1);
        /*
         * Add n1 with 2's complement of n2 continuosly
         * until n1 becomes lesser value than n2.
         * Division of two numbers without using arithmetic
         * operator.
         */
        for (i = 0; res > 0; i++) {
                res = binaryAddition(res, twosComplement);
                if (res <= 0) {
                        if (res == 0)
                                count = binaryAddition(count, 1);
                        break;
                } else {
                        count = binaryAddition(count, 1);
                }
        }
        return count;
  }


  int main() {
        int n1, n2, res, ch;
        while (1) {
                printf("1. Binary Addition\n");
                printf("2. Binary Subtraction\n");
                printf("3. Binary Multiplication\n");
                printf("4. Binary Division\n5. Exit\n");
                printf("Enter your choice:");
                scanf("%d", &ch);

                /* if choice is not exit, get the inputs n1 and n2 */
                if (ch != 5) {
                        printf("Enter the value for n1 and n2:");
                        scanf("%d%d", &n1, &n2);
                }

                switch(ch) {
                        case 1:
                                /* binary addition */
                                res = binaryAddition(n1, n2);
                                break;
                        case 2:
                                /* binary subtraction */
                                res = binarySubtracton(n1, n2);
                                break;
                        case 3:
                                /* binary multiplication */
                                res = binaryMultiplication(n1, n2);
                                break;
                        case 4:
                                /* binary division */
                                res = binaryDivision(n1, n2);
                                break;
                        case 5:
                                exit(0);
                        default:
                                printf("Wrong option!!\n");
                                break;
                }
                printf("Result: %d\n\n", res);
        }
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  1. Binary Addition
  2. Binary Subtraction
  3. Binary Multiplication
  4. Binary Division
  5. Exit
  Enter your choice:1
  Enter the value for n1 and n2:10 20
  Result: 30

  1. Binary Addition
  2. Binary Subtraction
  3. Binary Multiplication
  4. Binary Division
  5. Exit
  Enter your choice:2   
  Enter the value for n1 and n2:30 8
  Result: 22

  1. Binary Addition
  2. Binary Subtraction
  3. Binary Multiplication
  4. Binary Division
  5. Exit
  Enter your choice:3
  Enter the value for n1 and n2:44 20
  Result: 880

  1. Binary Addition
  2. Binary Subtraction
  3. Binary Multiplication
  4. Binary Division
  5. Exit
  Enter your choice:4
  Enter the value for n1 and n2:88 22
  Result: 4

  1. Binary Addition
  2. Binary Subtraction
  3. Binary Multiplication
  4. Binary Division
  5. Exit
  Enter your choice:5
  Enter the value for n1 and n2:5   



No comments:

Post a Comment