This question was asked in Qualcomm. without using arithmetic operations add two numbers. We can use bitwise operators to add two numbers.

 
#include <iostream>
 
using namespace std;
 
int sum(int a, int b);
 
int main()
{
   int a = 1, b = 0;
   int sum_ = 0;
   sum_ = sum(a, b);
   cout << sum_; 
}
 
int sum(int x, int y)
{
if( x == y)
  return (x << 1); /*if numbers are same return 2*number*/
else 
return (x ^ y);
}
Shahid Siddique software engineer and WordPress adept. He works with Cerner Corp as a Software Engineer.
Shahid Siddique
View all posts by Shahid Siddique
Shahids website

3 Comments to “Add two numbers without using (+, -, /, *)”

  • What about 1+3?

    • Thank you stephen for your comment. I will look into this and get back to you with the solution.

    • So I came across one solution: http://www.daniweb.com/software-development/c/threads/84950/adding-two-numbers

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      
      /*
      integer1 =  3  = 0011b
      integer2 =  1  = 0001b
          first operation        second operation      third operation
              0011                0011                 shift by one
              0001                0001                   
             ______              ______
       XOR    0010           AND  0001  ------>        <<1   0010
          Again...  
                         first operation   second operation   third operation
      previous XOR        0010                0010            shift by one
      previous <<1        0100
          Again...  
                         first operation  second operation    third operation
      previous XOR        0000                0000            shift by one
      previous <<1        0000
       
      When AND equals 0 the result of XOR is is the result
       
      Result is 100 which is 4 
      */
       
      #include <iostream>
       
      using namespace std;
       
       
      unsigned long add(unsigned long integer1, unsigned long integer2)
      {
      unsigned long xor, and, temp;
       
      and = integer1 & integer2; /* Obtain the carry bits */
      xor = integer1 ^ integer2; /* result­ing bits */
       
      while(and != 0 ) /* stop when carry bits are gone */
      {
      and <<= 1; /* shift­ing the carry bits one space */
      temp = xor ^ and; /* hold the new xor result bits*/
      and &= xor; /* clear the pre­vi­ous carry bits and assign the new carry bits */
      xor = temp; /* result­ing bits */
      }
      return xor; /* final result */
      }
       
      int main()
      {
      	cout <<add(3,3);
      }

Post comment


eight × 2 =