Add two numbers using bitwise operators

Here given code implementation process.

// Java program
// Add two numbers using bitwise operators
public class NumberAddition
{
	// Add two numbers
	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;
		}
		// Display add result
		System.out.print(" : " + a);
	}
	public static void main(String[] args)
	{
		NumberAddition task = new NumberAddition();
		// Test Case
		task.addition(8, -2);
		task.addition(5, 3);
		task.addition(-3, -5);
	}
}

Output

 [(8) + (-2)]  : 6
 [(5) + (3)]  : 8
 [(-3) + (-5)]  : -8
// Include header file
#include <iostream>
using namespace std;
// C++ program
// Add two numbers using bitwise operators
class NumberAddition
{
	public:
		// Add two numbers
		void addition(int a, int b)
		{
			// 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;
			}
			// Display add result
			cout << " : " << a;
		}
};
int main()
{
	NumberAddition *task = new NumberAddition();
	// Test Case
	task->addition(8, -2);
	task->addition(5, 3);
	task->addition(-3, -5);
	return 0;
}

Output

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

// Add two numbers
void addition(int a, int b)
{
	// 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;
	}
	// Display add result
	printf(" : %d ", a);
}
int main()
{
	// Test Case
	addition(8, -2);
	addition(5, 3);
	addition(-3, -5);
	return 0;
}

Output

 [(8) + (-2)]  : 6
 [(5) + (3)]  : 8
 [(-3) + (-5)]  : -8
package main
import "fmt"
// Go program
// Add two numbers using bitwise operators
type NumberAddition struct {}
func getNumberAddition() * NumberAddition {
	var me *NumberAddition = &NumberAddition {}
	return me
}
// Add two numbers
func(this NumberAddition) addition(a, b int) {
	// 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
	}
	// Display add result
	fmt.Print(" : ", a)
}
func main() {
	var task * NumberAddition = getNumberAddition()
	// Test Case
	task.addition(8, -2)
	task.addition(5, 3)
	task.addition(-3, -5)
}

Output

 [(8) + (-2)]  : 6
 [(5) + (3)]  : 8
 [(-3) + (-5)]  : -8
// Include namespace system
using System;
// Csharp program
// Add two numbers using bitwise operators
public class NumberAddition
{
	// Add two numbers
	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;
		}
		// Display add result
		Console.Write(" : " + a);
	}
	public static void Main(String[] args)
	{
		NumberAddition task = new NumberAddition();
		// Test Case
		task.addition(8, -2);
		task.addition(5, 3);
		task.addition(-3, -5);
	}
}

Output

 [(8) + (-2)]  : 6
 [(5) + (3)]  : 8
 [(-3) + (-5)]  : -8
<?php
// Php program
// Add two numbers using bitwise operators
class NumberAddition
{
	// Add two numbers
	public	function addition($a, $b)
	{
		// 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;
		}
		// Display add result
		echo(" : ".$a);
	}
}

function main()
{
	$task = new NumberAddition();
	// Test Case
	$task->addition(8, -2);
	$task->addition(5, 3);
	$task->addition(-3, -5);
}
main();

Output

 [(8) + (-2)]  : 6
 [(5) + (3)]  : 8
 [(-3) + (-5)]  : -8
// Node JS program
// Add two numbers using bitwise operators
class NumberAddition
{
	// Add two numbers
	addition(a, b)
	{
		// 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;
		}
		// Display add result
		process.stdout.write(" : " + a);
	}
}

function main()
{
	var task = new NumberAddition();
	// Test Case
	task.addition(8, -2);
	task.addition(5, 3);
	task.addition(-3, -5);
}
main();

Output

 [(8) + (-2)]  : 6
 [(5) + (3)]  : 8
 [(-3) + (-5)]  : -8
// Swift 4 program
// Add two numbers using bitwise operators
class NumberAddition
{
	// Add two numbers
	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;
		}
		// Display add result
		print(" : ", a, terminator: "");
	}
}
func main()
{
	let task: NumberAddition = NumberAddition();
	// Test Case
	task.addition(8, -2);
	task.addition(5, 3);
	task.addition(-3, -5);
}
main();

Output

 [( 8 ) + ( -2 )]  :  6
 [( 5 ) + ( 3 )]  :  8
 [( -3 ) + ( -5 )]  :  -8
// Kotlin program
// Add two numbers using bitwise operators
class NumberAddition
{
	// Add two numbers
	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;
		}
		// Display add result
		print(" : " + a);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: NumberAddition = NumberAddition();
	// Test Case
	task.addition(8, -2);
	task.addition(5, 3);
	task.addition(-3, -5);
}

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.

New Comment







© 2021, kalkicode.com, All rights reserved