# Add two numbers using bitwise operators

Here given code implementation process.

// Java program
// Add two numbers using bitwise operators
{
public void addition(int a, int b)
{
// Display given numbers
System.out.print("\n [(" + a + ") + (" + b + ")] ");
// Auxiliary variable which are store the carry result
int carry_bit = 0;
// Executing loop, until b value is not zero
while (b != 0)
{
// Get carry bits of a & b
carry_bit = a & b;
// Sum of bits of a ^ b
a = a ^ b;
// Shift the carry bit by one bit in left side
b = carry_bit << 1;
}
System.out.print(" : " + a);
}
public static void main(String[] args)
{
// Test Case
}
}

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8
#include <iostream>
using namespace std;
// C++ program
// Add two numbers using bitwise operators
{
public:
{
// Display given numbers
cout << "\n [(" << a << ") + (" << b << ")] ";
// Auxiliary variable which are store the carry result
int carry_bit = 0;
// Executing loop, until b value is not zero
while (b != 0)
{
// Get carry bits of a &b
carry_bit = a &b;
// Sum of bits of a ^ b
a = a ^ b;
// Shift the carry bit by one bit in left side
b = carry_bit << 1;
}
cout << " : " << a;
}
};
int main()
{
// Test Case
return 0;
}

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8
// C Program
// Add two numbers using bitwise operators
#include <stdio.h>

{
// Display given numbers
printf("\n [(%d) + (%d)] ", a, b);
// Auxiliary variable which are store the carry result
int carry_bit = 0;
// Executing loop, until b value is not zero
while (b != 0)
{
// Get carry bits of a & b
carry_bit = a & b;
// Sum of bits of a ^ b
a = a ^ b;
// Shift the carry bit by one bit in left side
b = carry_bit << 1;
}
printf(" : %d ", a);
}
int main()
{
// Test Case
return 0;
}

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8
package main
import "fmt"
// Go program
// Add two numbers using bitwise operators
return me
}
// Display given numbers
fmt.Print("\n [(", a, ") + (", b, ")] ")
// Auxiliary variable which are store the carry result
var carry_bit int = 0
// Executing loop, until b value is not zero
for (b != 0) {
// Get carry bits of a & b
carry_bit = a & b
// Sum of bits of a ^ b
a = a ^ b
// Shift the carry bit by one bit in left side
b = carry_bit << 1
}
fmt.Print(" : ", a)
}
func main() {
// Test Case
}

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8
// Include namespace system
using System;
// Csharp program
// Add two numbers using bitwise operators
{
public void addition(int a, int b)
{
// Display given numbers
Console.Write("\n [(" + a + ") + (" + b + ")] ");
// Auxiliary variable which are store the carry result
int carry_bit = 0;
// Executing loop, until b value is not zero
while (b != 0)
{
// Get carry bits of a & b
carry_bit = a & b;
// Sum of bits of a ^ b
a = a ^ b;
// Shift the carry bit by one bit in left side
b = carry_bit << 1;
}
Console.Write(" : " + a);
}
public static void Main(String[] args)
{
// Test Case
}
}

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8
<?php
// Php program
// Add two numbers using bitwise operators
{
{
// Display given numbers
echo("\n [(".\$a.") + (".\$b.")] ");
// Auxiliary variable which are store the carry result
\$carry_bit = 0;
// Executing loop, until b value is not zero
while (\$b != 0)
{
// Get carry bits of a & b
\$carry_bit = \$a & \$b;
// Sum of bits of a ^ b
\$a = \$a ^ \$b;
// Shift the carry bit by one bit in left side
\$b = \$carry_bit << 1;
}
echo(" : ".\$a);
}
}

function main()
{
// Test Case
}
main();

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8
// Node JS program
// Add two numbers using bitwise operators
{
{
// Display given numbers
process.stdout.write("\n [(" + a + ") + (" + b + ")] ");
// Auxiliary variable which are store the carry result
var carry_bit = 0;
// Executing loop, until b value is not zero
while (b != 0)
{
// Get carry bits of a & b
carry_bit = a & b;
// Sum of bits of a ^ b
a = a ^ b;
// Shift the carry bit by one bit in left side
b = carry_bit << 1;
}
process.stdout.write(" : " + a);
}
}

function main()
{
// Test Case
}
main();

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8
// Swift 4 program
// Add two numbers using bitwise operators
{
func addition(_ n1: Int, _ n2: Int)
{
var a = n1;
var b = n2;
// Display given numbers
print("\n [(", a ,") + (", b ,")] ", terminator: "");
// Auxiliary variable which are store the carry result
var carry_bit: Int = 0;
// Executing loop, until b value is not zero
while (b  != 0)
{
// Get carry bits of a & b
carry_bit = a & b;
// Sum of bits of a ^ b
a = a ^ b;
// Shift the carry bit by one bit in left side
b = carry_bit << 1;
}
print(" : ", a, terminator: "");
}
}
func main()
{
// Test Case
}
main();

#### Output

[( 8 ) + ( -2 )]  :  6
[( 5 ) + ( 3 )]  :  8
[( -3 ) + ( -5 )]  :  -8
// Kotlin program
// Add two numbers using bitwise operators
{
fun addition(n1: Int, n2: Int): Unit
{
var a = n1;
var b = n2;
// Display given numbers
print("\n [(" + a + ") + (" + b + ")] ");
// Auxiliary variable which are store the carry result
var carry_bit: Int ;
// Executing loop, until b value is not zero
while (b != 0)
{
// Get carry bits of a & b
carry_bit = a and b;
// Sum of bits of a ^ b
a = a xor b;
// Shift the carry bit by one bit in left side
b = carry_bit shl 1;
}
print(" : " + a);
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}

#### Output

[(8) + (-2)]  : 6
[(5) + (3)]  : 8
[(-3) + (-5)]  : -8

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.