Add Two Numbers Without Arithmetic Operators in C#

Add Two Numbers Without Arithmetic Operators in C Sharp – Add Two Numbers – Learn how to add 2 numbers without using + operator

To calculate the sum, we can use XOR operator. This XOR operator is a bitwise operator that perform addition operation on bits. if over flow occurs we can take that as carry and forward it as sum to next bit operation.

The above scenario is – Half Adder logic .

  • Half adder logic contains SUM and Carry.
  • sum is XOR(^)  operation of 2 bits.
  • carry is AND(&)  operation of 2 bits.

For example there are 2 numbers num1,mum2 where num1=3, num2=4,

then XOR operation of num1, num2 is 7 and  carry is 0.

XOR(num1,num2)=num1^num2=3^4=7
carry =0

In the above scenario num1 and num2 bits are not set in the same position, so we can not find any carry for that.

For example there are 2 numbers num1,mum2 where num1=4, num2=4 then XOR operation of num1, num2 is 0 and carry is 1, because the num1 and num2 bits are in the same position.

XOR(num1,num2)=num1^num2=4^4=0
carry =1

Add Two Numbers Without Arithmetic Operators in C Sharp

C# Example program to add 2 numbers using XOR and AND operators.

Method – 1

using System;
class Program
{
static void Main(string[] args)
{
int num1 = 4, num2 = 4, carry, sum;

while (true)
{
//step 1: Add num1 and num2 but do not add
// carry and store in variable sum
sum = num1 ^ num2; //calculating sum of 2 numbers
//step 2: perform and opperation to calculate the carry
carry = num1 & num2; //calculating carry for 2 numbers

if (carry == 0)
break;
else
{
//if carry contains non zero value 
//then forward the carry from right to left 
//using left shift operation.
carry = carry << 1;
num1 = sum;

// forwarding the carry to next bit by 
//using left shift operation
num2 = carry; 
}

}
Console.WriteLine("\n Addition of Two Numbers 
          num1 and num2 = " + sum);
}
}

Method – 2

using System;
class Program
{
static void Main(string[] args)
{
int a, b, num1=4, num2=4, carry, sum;

a = num1;
b = num2;
while (num2 != 0)
{
//calculating carry for 2 numbers
carry = num1 & num2; 

//calculating sum of 2 numbers
num1 = num1 ^ num2; 

// forwarding the carry 
//to next bit by using left shift operation
num2 = carry << 1; 
}

//assigning num1 value to sum
sum = num1;
Console.WriteLine("\n Addition of Two Numbers
 num1 and num2 = "+ sum);

}
}

Output

Addition of Two Numbers num1 and num2 = 8

C# Example program to add 2 numbers using XOR and AND operators with recursive function

class Program
{
public int carry=1, sum;
static void Main(string[] args)
{
int num1 = 4, num2 = 4,sum;

Program p = new Program();
sum= p.Add(num1, num2);
Console.WriteLine("\n Addition of Two Numbers
 num1 and num2 = " + sum);
}
int Add(int a, int b)
{
if (carry == 0)
return sum;
else
{
//step 1: Add a and b but do not add carry 
//and store in variable sum
sum = a ^ b;

//step 2: Now add again but this time list out the carry’s 
//and append 0 at end and store in list carry

carry = a & b;

//left shift operator(<<)to append 0 at end
carry = carry << 1;
a = sum;
b = carry;
return Add(a, b);
}
}
}

Example program – Addition of 2 numbers using  ‘-‘ operator

class Program
{
public int carry = 1, sum;
static void Main(string[] args)
{
int num1 = 4, num2 = 4,sum;

sum = num1 - (-num2);

Console.WriteLine("\n Addition of Two Numbers
 num1 and num2 = " + sum);
}
}

Notes

The above Example have Arithmetic Operator [-] but this example is for “Adding two numbers without using + Operator “

Example program – sum of 2 numbers using  ++ and –operators

class Program
{
public int carry = 1, sum;
static void Main(string[] args)
{
int num1 = 4, num2 = 4, sum;

while (num2 > 0)
{
num1++;
num2--;
}
sum = num1;

Console.WriteLine("\n Addition of Two Numbers 
num1 and num2 = " + sum);
}
}

Example program – with ++ and –operators using function

class Program
{
public int carry = 1, sum;
static void Main(string[] args)
{
int num1 = 4, num2 = 4, sum;

Program p = new Program();

sum = p.add(num1,num2);

Console.WriteLine("\n Addition of Two Numbers 
num1 and num2 = " + sum);
}
int add(int num1, int num2)
{
int i;
for (i = 0; i < num2; i++)
num1++;
return num1;
}
}

Example program – sum of 2 numbers using recursive function

class Program
{
public int carry = 1, sum;
static void Main(string[] args)
{
int num1 = 3, num2 = 4, sum;

Program p = new Program();

sum = p.add(num1, num2);

Console.WriteLine("\n Addition of Two Numbers 
num1 and num2 = " + sum);
}
int add(int num1, int num2)
{
if (num1==0)
return num2;
else
return add((num1 & num2) << 1, num1 ^ num2);
}
}

Related Posts