Find xor of two numbers without using xor operator

Here given code implementation process.

// C Program
// Find xor of two numbers without using xor operator
#include <stdio.h>

void findXor(int a, int b)
{
    printf("\n Given a : %d  b : %d", a, b);
    // Example 1
    // (a | b) find all active bits
    // (a & b) find common active bits
    // (a | b) - (a & b) subtract common bits
    int result = (a | b) - (a & b);
    printf("\n ➀ (%d | %d) - (%d & %d) : %d", a, b, a, b, result);
    // Example 2
    result = (a & ~b) | (~a & b);
    printf("\n ➁ (%d & ~%d) | (~%d & %d) : %d", a, b, a, b, result);
    // Example 3
    result = (a | b) & (~a | ~b);
    printf("\n ➂ (%d | %d) & (~%d | ~%d) : %d", a, b, a, b, result);
    // Example 4
    result = -1 - ((a & b) | (~a & ~b));
    printf("\n ➃ -1 - (%d & %d) | (~%d & ~%d) : %d", a, b, a, b, result);
    // Example 5
    result = ((a & ~b) | (~a & b));
    printf("\n ➄ (%d & ~%d) | (~%d & %d) : %d", a, b, a, b, result);
}
int main()
{
    // Test
    findXor(15, 19);
    findXor(44, 39);
    findXor(-5, -9);
    return 0;
}

Output

 Given a : 15  b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44  b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5  b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12
/*
    Java program
    Find xor of two numbers without using xor operator
*/
public class XorOperation
{
	public void findXor(int a, int b)
	{
		System.out.print("\n Given a : " + a + " b : " + b );
		// Example 1
		// (a | b) find all active bits
		// (a & b) find common active bits
		// (a | b) - (a & b) subtract common bits
		int result = (a | b) - (a & b);
		System.out.print("\n ➀ (" + a + " | " + b + ") - (" + 
                         a + " & " + b + ") : " + result );
		// Example 2
		result = (a & ~b ) | (~a & b);
		System.out.print("\n ➁ (" + a + " & ~" + b + ") | (~" + 
                         a + " & " + b + ") : " + result );
		// Example 3
		result = (a | b) & (~a | ~b );
		System.out.print("\n ➂ (" + a + " | " + b + ") & (~" + 
                         a + " | ~" + b + ") : " + result );
		// Example 4
		result = -1 - ((a & b) | (~a & ~b ));
		System.out.print("\n ➃ -1 - (" + a + " & " + b + ") | (~" + 
                         a + " & ~" + b + ") : " + result );
		// Example 5
		result = ((a & ~b ) | (~a & b));
		System.out.print("\n ➄ (" + a + " & ~" + b + ") | (~" + 
                         a + " & " + b + ") : " + result );
	}
	public static void main(String[] args)
	{
		XorOperation task = new XorOperation();
		// Test
		task.findXor(15, 19);
		task.findXor(44, 39);
		task.findXor(-5, -9);

	}
}

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program
    Find xor of two numbers without using xor operator
*/
class XorOperation
{
	public: void findXor(int a, int b)
	{
		cout << "\n Given a : " << a << " b : " << b;
		// Example 1
		// (a | b) find all active bits
		// (a &b) find common active bits
		// (a | b) - (a &b) subtract common bits
		int result = (a | b) - (a &b);
		cout << "\n ➀ (" << a << " | " << b << ") - (" 
      		 << a << " &" << b << ") : " << result;
		// Example 2
		result = (a &~b) | (~a &b);
		cout << "\n ➁ (" << a << " &~" << b << ") | (~" 
      		 << a << " &" << b << ") : " << result;
		// Example 3
		result = (a | b) &(~a | ~b);
		cout << "\n ➂ (" << a << " | " << b << ") &(~" 
      		 << a << " | ~" << b << ") : " << result;
		// Example 4
		result = -1 - ((a &b) | (~a &~b));
		cout << "\n ➃ -1 - (" << a << " &" << b << ") | (~" 
      		 << a << " &~" << b << ") : " << result;
		// Example 5
		result = ((a &~b) | (~a &b));
		cout << "\n ➄ (" << a << " &~" << b << ") | (~" 
      		 << a << " &" << b << ") : " << result;
	}
};
int main()
{
	XorOperation *task = new XorOperation();
	// Test
	task->findXor(15, 19);
	task->findXor(44, 39);
	task->findXor(-5, -9);
	return 0;
}

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 &19) : 28
 ➁ (15 &~19) | (~15 &19) : 28
 ➂ (15 | 19) &(~15 | ~19) : 28
 ➃ -1 - (15 &19) | (~15 &~19) : 28
 ➄ (15 &~19) | (~15 &19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 &39) : 11
 ➁ (44 &~39) | (~44 &39) : 11
 ➂ (44 | 39) &(~44 | ~39) : 11
 ➃ -1 - (44 &39) | (~44 &~39) : 11
 ➄ (44 &~39) | (~44 &39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 &-9) : 12
 ➁ (-5 &~-9) | (~-5 &-9) : 12
 ➂ (-5 | -9) &(~-5 | ~-9) : 12
 ➃ -1 - (-5 &-9) | (~-5 &~-9) : 12
 ➄ (-5 &~-9) | (~-5 &-9) : 12
// Include namespace system
using System;
/*
    Csharp program
    Find xor of two numbers without using xor operator
*/
public class XorOperation
{
	public void findXor(int a, int b)
	{
		Console.Write("\n Given a : " + a + " b : " + b);
		// Example 1
		// (a | b) find all active bits
		// (a & b) find common active bits
		// (a | b) - (a & b) subtract common bits
		int result = (a | b) - (a & b);
		Console.Write("\n ➀ (" + a + " | " + b + ") - (" + 
                      a + " & " + b + ") : " + result);
		// Example 2
		result = (a & ~b) | (~a & b);
		Console.Write("\n ➁ (" + a + " & ~" + b + ") | (~" + 
                      a + " & " + b + ") : " + result);
		// Example 3
		result = (a | b) & (~a | ~b);
		Console.Write("\n ➂ (" + a + " | " + b + ") & (~" + 
                      a + " | ~" + b + ") : " + result);
		// Example 4
		result = -1 - ((a & b) | (~a & ~b));
		Console.Write("\n ➃ -1 - (" + a + " & " + b + ") | (~" + 
                      a + " & ~" + b + ") : " + result);
		// Example 5
		result = ((a & ~b) | (~a & b));
		Console.Write("\n ➄ (" + a + " & ~" + b + ") | (~" + 
                      a + " & " + b + ") : " + result);
	}
	public static void Main(String[] args)
	{
		XorOperation task = new XorOperation();
		// Test
		task.findXor(15, 19);
		task.findXor(44, 39);
		task.findXor(-5, -9);
	}
}

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12
<?php
/*
    Php program
    Find xor of two numbers without using xor operator
*/
class XorOperation
{
	public	function findXor($a, $b)
	{
		echo("\n Given a : ".$a.
			" b : ".$b);
		// Example 1
		// (a | b) find all active bits
		// (a & b) find common active bits
		// (a | b) - (a & b) subtract common bits
		$result = ($a | $b) - ($a & $b);
		echo("\n ➀ (".$a.
			" | ".$b.
			") - (".$a.
			" & ".$b.
			") : ".$result);
		// Example 2
		$result = ($a & ~$b) | (~$a & $b);
		echo("\n ➁ (".$a.
			" & ~".$b.
			") | (~".$a.
			" & ".$b.
			") : ".$result);
		// Example 3
		$result = ($a | $b) & (~$a | ~$b);
		echo("\n ➂ (".$a.
			" | ".$b.
			") & (~".$a.
			" | ~".$b.
			") : ".$result);
		// Example 4
		$result = -1 - (($a & $b) | (~$a & ~$b));
		echo("\n ➃ -1 - (".$a.
			" & ".$b.
			") | (~".$a.
			" & ~".$b.
			") : ".$result);
		// Example 5
		$result = (($a & ~$b) | (~$a & $b));
		echo("\n ➄ (".$a.
			" & ~".$b.
			") | (~".$a.
			" & ".$b.
			") : ".$result);
	}
}

function main()
{
	$task = new XorOperation();
	// Test
	$task->findXor(15, 19);
	$task->findXor(44, 39);
	$task->findXor(-5, -9);
}
main();

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12
/*
    Node JS program
    Find xor of two numbers without using xor operator
*/
class XorOperation
{
	findXor(a, b)
	{
		process.stdout.write("\n Given a : " + a + " b : " + b);
		// Example 1
		// (a | b) find all active bits
		// (a & b) find common active bits
		// (a | b) - (a & b) subtract common bits
		var result = (a | b) - (a & b);
		process.stdout.write("\n ➀ (" + a + " | " + b + ") - (" + 
                             a + " & " + b + ") : " + result);
		// Example 2
		result = (a & ~b) | (~a & b);
		process.stdout.write("\n ➁ (" + a + " & ~" + b + ") | (~" + 
                             a + " & " + b + ") : " + result);
		// Example 3
		result = (a | b) & (~a | ~b);
		process.stdout.write("\n ➂ (" + a + " | " + b + ") & (~" + 
                             a + " | ~" + b + ") : " + result);
		// Example 4
		result = -1 - ((a & b) | (~a & ~b));
		process.stdout.write("\n ➃ -1 - (" + a + " & " + b + ") | (~" + 
                             a + " & ~" + b + ") : " + result);
		// Example 5
		result = ((a & ~b) | (~a & b));
		process.stdout.write("\n ➄ (" + a + " & ~" + b + ") | (~" + 
                             a + " & " + b + ") : " + result);
	}
}

function main()
{
	var task = new XorOperation();
	// Test
	task.findXor(15, 19);
	task.findXor(44, 39);
	task.findXor(-5, -9);
}
main();

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12
#    Python 3 program
#    Find xor of two numbers without using xor operator
class XorOperation :
	def findXor(self, a, b) :
		print("\n Given a : ", a ," b : ", b, end = "")
		#  Example 1
		#  (a | b) find all active bits
		#  (a & b) find common active bits
		#  (a | b) - (a & b) subtract common bits
		result = (a | b) - (a & b)
		print("\n ➀ (", a ," | ", b ,") - (", 
              a ," & ", b ,") : ", result, end = "")
		#  Example 2
		result = (a & ~b) | (~a & b)
		print("\n ➁ (", a ," & ~", b ,") | (~", 
              a ," & ", b ,") : ", result, end = "")
		#  Example 3
		result = (a | b) & (~a | ~b)
		print("\n ➂ (", a ," | ", b ,") & (~", 
              a ," | ~", b ,") : ", result, end = "")
		#  Example 4
		result = -1 - ((a & b) | (~a & ~b))
		print("\n ➃ -1 - (", a ," & ", b ,") | (~", 
              a ," & ~", b ,") : ", result, end = "")
		#  Example 5
		result = ((a & ~b) | (~a & b))
		print("\n ➄ (", a ," & ~", b ,") | (~", 
              a ," & ", b ,") : ", result, end = "")
	

def main() :
	task = XorOperation()
	#  Test
	task.findXor(15, 19)
	task.findXor(44, 39)
	task.findXor(-5, -9)

if __name__ == "__main__": main()

Output

 Given a :  15  b :  19
 ➀ ( 15  |  19 ) - ( 15  &  19 ) :  28
 ➁ ( 15  & ~ 19 ) | (~ 15  &  19 ) :  28
 ➂ ( 15  |  19 ) & (~ 15  | ~ 19 ) :  28
 ➃ -1 - ( 15  &  19 ) | (~ 15  & ~ 19 ) :  28
 ➄ ( 15  & ~ 19 ) | (~ 15  &  19 ) :  28
 Given a :  44  b :  39
 ➀ ( 44  |  39 ) - ( 44  &  39 ) :  11
 ➁ ( 44  & ~ 39 ) | (~ 44  &  39 ) :  11
 ➂ ( 44  |  39 ) & (~ 44  | ~ 39 ) :  11
 ➃ -1 - ( 44  &  39 ) | (~ 44  & ~ 39 ) :  11
 ➄ ( 44  & ~ 39 ) | (~ 44  &  39 ) :  11
 Given a :  -5  b :  -9
 ➀ ( -5  |  -9 ) - ( -5  &  -9 ) :  12
 ➁ ( -5  & ~ -9 ) | (~ -5  &  -9 ) :  12
 ➂ ( -5  |  -9 ) & (~ -5  | ~ -9 ) :  12
 ➃ -1 - ( -5  &  -9 ) | (~ -5  & ~ -9 ) :  12
 ➄ ( -5  & ~ -9 ) | (~ -5  &  -9 ) :  12
#    Ruby program
#    Find xor of two numbers without using xor operator
class XorOperation 
	def findXor(a, b) 
		print("\n Given a : ", a ," b : ", b)
		#  Example 1
		#  (a | b) find all active bits
		#  (a & b) find common active bits
		#  (a | b) - (a & b) subtract common bits
		result = (a | b) - (a & b)
		print("\n ➀ (", a ," | ", b ,") - (", 
              a ," & ", b ,") : ", result)
		#  Example 2
		result = (a & ~b) | (~a & b)
		print("\n ➁ (", a ," & ~", b ,") | (~", 
              a ," & ", b ,") : ", result)
		#  Example 3
		result = (a | b) & (~a | ~b)
		print("\n ➂ (", a ," | ", b ,") & (~", 
              a ," | ~", b ,") : ", result)
		#  Example 4
		result = -1 - ((a & b) | (~a & ~b))
		print("\n ➃ -1 - (", a ," & ", b ,") | (~", 
              a ," & ~", b ,") : ", result)
		#  Example 5
		result = ((a & ~b) | (~a & b))
		print("\n ➄ (", a ," & ~", b ,") | (~", 
              a ," & ", b ,") : ", result)
	end

end

def main() 
	task = XorOperation.new()
	#  Test
	task.findXor(15, 19)
	task.findXor(44, 39)
	task.findXor(-5, -9)
end

main()

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12
/*
    Scala program
    Find xor of two numbers without using xor operator
*/
class XorOperation()
{
	def findXor(a: Int, b: Int): Unit = {
		print("\n Given a : " + a + " b : " + b);
		// Example 1
		// (a | b) find all active bits
		// (a & b) find common active bits
		// (a | b) - (a & b) subtract common bits
		var result: Int = (a | b) - (a & b);
		print("\n ➀ (" + a + " | " + b + ") - (" + 
              a + " & " + b + ") : " + result);
		// Example 2
		result = (a & ~b) | (~a & b);
		print("\n ➁ (" + a + " & ~" + b + ") | (~" + 
              a + " & " + b + ") : " + result);
		// Example 3
		result = (a | b) & (~a | ~b);
		print("\n ➂ (" + a + " | " + b + ") & (~" + 
              a + " | ~" + b + ") : " + result);
		// Example 4
		result = -1 - ((a & b) | (~a & ~b));
		print("\n ➃ -1 - (" + a + " & " + b + ") | (~" + 
              a + " & ~" + b + ") : " + result);
		// Example 5
		result = ((a & ~b) | (~a & b));
		print("\n ➄ (" + a + " & ~" + b + ") | (~" + 
              a + " & " + b + ") : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: XorOperation = new XorOperation();
		// Test
		task.findXor(15, 19);
		task.findXor(44, 39);
		task.findXor(-5, -9);
	}
}

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12
/*
    Swift 4 program
    Find xor of two numbers without using xor operator
*/
class XorOperation
{
	func findXor(_ a: Int, _ b: Int)
	{
		print("\n Given a : ", a ," b : ", b, terminator: "");
		// Example 1
		// (a | b) find all active bits
		// (a & b) find common active bits
		// (a | b) - (a & b) subtract common bits
		var result: Int = (a | b) - (a & b);
		print("\n ➀ (", a ," | ", b ,") - (", 
              a ," & ", b ,") : ", result, terminator: "");
		// Example 2
		result = (a & ~b) | (~a & b);
		print("\n ➁ (", a ," & ~", b ,") | (~", 
              a ," & ", b ,") : ", result, terminator: "");
		// Example 3
		result = (a | b) & (~a | ~b);
		print("\n ➂ (", a ," | ", b ,") & (~", 
              a ," | ~", b ,") : ", result, terminator: "");
		// Example 4
		result = -1 - ((a & b) | (~a & ~b));
		print("\n ➃ -1 - (", a ," & ", b ,") | (~", 
              a ," & ~", b ,") : ", result, terminator: "");
		// Example 5
		result = ((a & ~b) | (~a & b));
		print("\n ➄ (", a ," & ~", b ,") | (~", 
              a ," & ", b ,") : ", result, terminator: "");
	}
}
func main()
{
	let task: XorOperation = XorOperation();
	// Test
	task.findXor(15, 19);
	task.findXor(44, 39);
	task.findXor(-5, -9);
}
main();

Output

 Given a :  15  b :  19
 ➀ ( 15  |  19 ) - ( 15  &  19 ) :  28
 ➁ ( 15  & ~ 19 ) | (~ 15  &  19 ) :  28
 ➂ ( 15  |  19 ) & (~ 15  | ~ 19 ) :  28
 ➃ -1 - ( 15  &  19 ) | (~ 15  & ~ 19 ) :  28
 ➄ ( 15  & ~ 19 ) | (~ 15  &  19 ) :  28
 Given a :  44  b :  39
 ➀ ( 44  |  39 ) - ( 44  &  39 ) :  11
 ➁ ( 44  & ~ 39 ) | (~ 44  &  39 ) :  11
 ➂ ( 44  |  39 ) & (~ 44  | ~ 39 ) :  11
 ➃ -1 - ( 44  &  39 ) | (~ 44  & ~ 39 ) :  11
 ➄ ( 44  & ~ 39 ) | (~ 44  &  39 ) :  11
 Given a :  -5  b :  -9
 ➀ ( -5  |  -9 ) - ( -5  &  -9 ) :  12
 ➁ ( -5  & ~ -9 ) | (~ -5  &  -9 ) :  12
 ➂ ( -5  |  -9 ) & (~ -5  | ~ -9 ) :  12
 ➃ -1 - ( -5  &  -9 ) | (~ -5  & ~ -9 ) :  12
 ➄ ( -5  & ~ -9 ) | (~ -5  &  -9 ) :  12
/*
    Kotlin program
    Find xor of two numbers without using xor operator
*/
class XorOperation
{
	fun findXor(a: Int, b: Int): Unit
	{
		print("\n Given a : " + a + " b : " + b);
		// Example 1
		// (a | b) find all active bits
		// (a & b) find common active bits
		// (a | b) - (a & b) subtract common bits
		var result: Int = (a or b) - (a and b);
		print("\n ➀ (" + a + " | " + b + ") - (" + 
              a + " & " + b + ") : " + result);
		// Example 2
		result = (a and b.inv()) or(a.inv() and b);
		print("\n ➁ (" + a + " & ~" + b + ") | (~" + 
              a + " & " + b + ") : " + result);
		// Example 3
		result = (a or b) and(a.inv() or b.inv());
		print("\n ➂ (" + a + " | " + b + ") & (~" + 
              a + " | ~" + b + ") : " + result);
		// Example 4
		result = -1 - ((a and b) or(a.inv() and b.inv()));
		print("\n ➃ -1 - (" + a + " & " + b + ") | (~" + 
              a + " & ~" + b + ") : " + result);
		// Example 5
		result = ((a and b.inv()) or(a.inv() and b));
		print("\n ➄ (" + a + " & ~" + b + ") | (~" + 
              a + " & " + b + ") : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: XorOperation = XorOperation();
	// Test
	task.findXor(15, 19);
	task.findXor(44, 39);
	task.findXor(-5, -9);
}

Output

 Given a : 15 b : 19
 ➀ (15 | 19) - (15 & 19) : 28
 ➁ (15 & ~19) | (~15 & 19) : 28
 ➂ (15 | 19) & (~15 | ~19) : 28
 ➃ -1 - (15 & 19) | (~15 & ~19) : 28
 ➄ (15 & ~19) | (~15 & 19) : 28
 Given a : 44 b : 39
 ➀ (44 | 39) - (44 & 39) : 11
 ➁ (44 & ~39) | (~44 & 39) : 11
 ➂ (44 | 39) & (~44 | ~39) : 11
 ➃ -1 - (44 & 39) | (~44 & ~39) : 11
 ➄ (44 & ~39) | (~44 & 39) : 11
 Given a : -5 b : -9
 ➀ (-5 | -9) - (-5 & -9) : 12
 ➁ (-5 & ~-9) | (~-5 & -9) : 12
 ➂ (-5 | -9) & (~-5 | ~-9) : 12
 ➃ -1 - (-5 & -9) | (~-5 & ~-9) : 12
 ➄ (-5 & ~-9) | (~-5 & -9) : 12


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