Perform XNOR of two numbers

Here given code implementation process.

// C Program
// Perform XNOR of two numbers
#include <stdio.h>

// Activate the all bits after most significant bit
int setAllBit(int a)
{
	int n = a;
	n = n | n >> 1;
	n = n | n >> 2;
	n = n | n >> 4;
	n = n | n >> 8;
	n = n | n >> 16;
	return n;
}
// Perform xnor operation of given two numbers
void xnor(int x, int y)
{
	int result = 0;
	if (x > y)
	{
		result = (setAllBit(x) ^ x) ^ y;
		printf("\n ( %d ⊙ %d ) :  %d", x, y, result);
	}
	else
	{
		result = (setAllBit(y) ^ y) ^ x;
		printf("\n ( %d ⊙ %d ) :  %d", y, x, result);
	}
}
int main()
{
	//     x = 12  (1100)
	//     y = 6   (0110)
	//     ===============
	// x ⊙ y = 5   (0101)  
	xnor(12, 6);
	//     x = 7   (00111)
	//     y = 18  (10010)
	//     ===============
	// x ⊙ y = 10  (01010)  
	xnor(7, 18);
	//     x = 45   (101101)
	//     y = 32   (100000)
	//     ===============
	// x ⊙ y = 50   (110010)  
	xnor(45, 32);
	return 0;
}

Output

 ( 12 ⊙ 6 ) :  5
 ( 18 ⊙ 7 ) :  10
 ( 45 ⊙ 32 ) :  50
/*
   Java Program
   Perform XNOR of two numbers
*/
public class ExclusiveNOR
{
	// Activate the all bits after most significant bit
	public int setAllBit(int a)
	{
		int n = a;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Perform xnor operation of given two numbers
	public void xnor(int x, int y)
	{
		int result = 0;
		if (x > y)
		{
			result = (setAllBit(x) ^ x) ^ y;
			System.out.print("\n ( " + x + " ⊙ " + y + " ) : " + result);
		}
		else
		{
			result = (setAllBit(y) ^ y) ^ x;
			System.out.print("\n ( " + y + " ⊙ " + x + " ) : " + result);
		}
	}
	public static void main(String[] args)
	{
		ExclusiveNOR task = new ExclusiveNOR();
		//     x = 12  (1100)
		//     y = 6   (0110)
		//     ===============
		// x ⊙ y = 5   (0101)  
		task.xnor(12, 6);
		//     x = 7   (00111)
		//     y = 18  (10010)
		//     ===============
		// x ⊙ y = 10  (01010)  
		task.xnor(7, 18);
		//     x = 45   (101101)
		//     y = 32   (100000)
		//     ===============
		// x ⊙ y = 50   (110010)  
		task.xnor(45, 32);
	}
}

Output

 ( 12 ⊙ 6 ) : 5
 ( 18 ⊙ 7 ) : 10
 ( 45 ⊙ 32 ) : 50
// Include header file
#include <iostream>

using namespace std;
/*
   C++ Program
   Perform XNOR of two numbers
*/
class ExclusiveNOR
{
	public:
		// Activate the all bits after most significant bit
		int setAllBit(int a)
		{
			int n = a;
			n = n | n >> 1;
			n = n | n >> 2;
			n = n | n >> 4;
			n = n | n >> 8;
			n = n | n >> 16;
			return n;
		}
	// Perform xnor operation of given two numbers
	void xnor(int x, int y)
	{
		int result = 0;
		if (x > y)
		{
			result = (this->setAllBit(x) ^ x) ^ y;
			cout << "\n ( " << x << " ⊙ " << y << " ) : " << result;
		}
		else
		{
			result = (this->setAllBit(y) ^ y) ^ x;
			cout << "\n ( " << y << " ⊙ " << x << " ) : " << result;
		}
	}
};
int main()
{
	ExclusiveNOR task = ExclusiveNOR();
	//     x = 12  (1100)
	//     y = 6   (0110)
	//     ===============
	// x ⊙ y = 5   (0101)
	task.xnor(12, 6);
	//     x = 7   (00111)
	//     y = 18  (10010)
	//     ===============
	// x ⊙ y = 10  (01010)
	task.xnor(7, 18);
	//     x = 45   (101101)
	//     y = 32   (100000)
	//     ===============
	// x ⊙ y = 50   (110010)
	task.xnor(45, 32);
	return 0;
}

Output

 ( 12 ⊙ 6 ) : 5
 ( 18 ⊙ 7 ) : 10
 ( 45 ⊙ 32 ) : 50
// Include namespace system
using System;
/*
   C# Program
   Perform XNOR of two numbers
*/
public class ExclusiveNOR
{
	// Activate the all bits after most significant bit
	public int setAllBit(int a)
	{
		int n = a;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Perform xnor operation of given two numbers
	public void xnor(int x, int y)
	{
		int result = 0;
		if (x > y)
		{
			result = (setAllBit(x) ^ x) ^ y;
			Console.Write("\n ( " + x + " ⊙ " + y + " ) : " + result);
		}
		else
		{
			result = (setAllBit(y) ^ y) ^ x;
			Console.Write("\n ( " + y + " ⊙ " + x + " ) : " + result);
		}
	}
	public static void Main(String[] args)
	{
		ExclusiveNOR task = new ExclusiveNOR();
		//     x = 12  (1100)
		//     y = 6   (0110)
		//     ===============
		// x ⊙ y = 5   (0101)
		task.xnor(12, 6);
		//     x = 7   (00111)
		//     y = 18  (10010)
		//     ===============
		// x ⊙ y = 10  (01010)
		task.xnor(7, 18);
		//     x = 45   (101101)
		//     y = 32   (100000)
		//     ===============
		// x ⊙ y = 50   (110010)
		task.xnor(45, 32);
	}
}

Output

 ( 12 ⊙ 6 ) : 5
 ( 18 ⊙ 7 ) : 10
 ( 45 ⊙ 32 ) : 50
<?php
/*
   Php Program
   Perform XNOR of two numbers
*/
class ExclusiveNOR
{
	// Activate the all bits after most significant bit
	public	function setAllBit($a)
	{
		$n = $a;
		$n = $n | $n >> 1;
		$n = $n | $n >> 2;
		$n = $n | $n >> 4;
		$n = $n | $n >> 8;
		$n = $n | $n >> 16;
		return $n;
	}
	// Perform xnor operation of given two numbers
	public	function xnor($x, $y)
	{
		$result = 0;
		if ($x > $y)
		{
			$result = ($this->setAllBit($x) ^ $x) ^ $y;
			echo "\n ( ". $x ." ⊙ ". $y ." ) : ". $result;
		}
		else
		{
			$result = ($this->setAllBit($y) ^ $y) ^ $x;
			echo "\n ( ". $y ." ⊙ ". $x ." ) : ". $result;
		}
	}
}

function main()
{
	$task = new ExclusiveNOR();
	//     x = 12  (1100)
	//     y = 6   (0110)
	//     ===============
	// x ⊙ y = 5   (0101)
	$task->xnor(12, 6);
	//     x = 7   (00111)
	//     y = 18  (10010)
	//     ===============
	// x ⊙ y = 10  (01010)
	$task->xnor(7, 18);
	//     x = 45   (101101)
	//     y = 32   (100000)
	//     ===============
	// x ⊙ y = 50   (110010)
	$task->xnor(45, 32);
}
main();

Output

 ( 12 ⊙ 6 ) : 5
 ( 18 ⊙ 7 ) : 10
 ( 45 ⊙ 32 ) : 50
#  Python 3 Program
#  Perform XNOR of two numbers

class ExclusiveNOR :
	#  Activate the all bits after most significant bit
	def setAllBit(self, a) :
		n = a
		n = n | n >> 1
		n = n | n >> 2
		n = n | n >> 4
		n = n | n >> 8
		n = n | n >> 16
		return n
	
	#  Perform xnor operation of given two numbers
	def xnor(self, x, y) :
		result = 0
		if (x > y) :
			result = (self.setAllBit(x) ^ x) ^ y
			print("\n (", x ,"⊙", y ,") : ", result, end = "")
		else :
			result = (self.setAllBit(y) ^ y) ^ x
			print("\n (", y ,"⊙", x ,") : ", result, end = "")
		
	

def main() :
	task = ExclusiveNOR()
	#      x = 12  (1100)
	#      y = 6   (0110)
	#      ===============
	#  x ⊙ y = 5   (0101)  
	task.xnor(12, 6)
	#      x = 7   (00111)
	#      y = 18  (10010)
	#      ===============
	#  x ⊙ y = 10  (01010)  
	task.xnor(7, 18)
	#      x = 45   (101101)
	#      y = 32   (100000)
	#      ===============
	#  x ⊙ y = 50   (110010)  
	task.xnor(45, 32)

if __name__ == "__main__": main()

Output

 ( 12 ⊙ 6 ) :  5
 ( 18 ⊙ 7 ) :  10
 ( 45 ⊙ 32 ) :  50
#    Ruby Program
#    Perform XNOR of two numbers

class ExclusiveNOR 
	#  Activate the all bits after most significant bit
	def setAllBit(a) 
		n = a
		n = n | n >> 1
		n = n | n >> 2
		n = n | n >> 4
		n = n | n >> 8
		n = n | n >> 16
		return n
	end

	#  Perform xnor operation of given two numbers
	def xnor(x, y) 
		result = 0
		if (x > y) 
			result = (self.setAllBit(x) ^ x) ^ y
			print("\n ( ", x ," ⊙ ", y ," ) : ", result)
		else 
			result = (self.setAllBit(y) ^ y) ^ x
			print("\n ( ", y ," ⊙ ", x ," ) : ", result)
		end

	end

end

def main() 
	task = ExclusiveNOR.new()
	#      x = 12  (1100)
	#      y = 6   (0110)
	#      ===============
	#  x ⊙ y = 5   (0101)  
	task.xnor(12, 6)
	#      x = 7   (00111)
	#      y = 18  (10010)
	#      ===============
	#  x ⊙ y = 10  (01010)  
	task.xnor(7, 18)
	#      x = 45   (101101)
	#      y = 32   (100000)
	#      ===============
	#  x ⊙ y = 50   (110010)  
	task.xnor(45, 32)
end

main()

Output

 ( 12 ⊙ 6 ) : 5
 ( 18 ⊙ 7 ) : 10
 ( 45 ⊙ 32 ) : 50
/*
   Scala Program
   Perform XNOR of two numbers
*/
class ExclusiveNOR
{
	// Activate the all bits after most significant bit
	def setAllBit(a: Int): Int = {
		var n: Int = a;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Perform xnor operation of given two numbers
	def xnor(x: Int, y: Int): Unit = {
		var result: Int = 0;
		if (x > y)
		{
			result = (this.setAllBit(x) ^ x) ^ y;
			print("\n ( " + x + " ⊙ " + y + " ) : " + result);
		}
		else
		{
			result = (this.setAllBit(y) ^ y) ^ x;
			print("\n ( " + y + " ⊙ " + x + " ) : " + result);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ExclusiveNOR = new ExclusiveNOR();
		//     x = 12  (1100)
		//     y = 6   (0110)
		//     ===============
		// x ⊙ y = 5   (0101)
		task.xnor(12, 6);
		//     x = 7   (00111)
		//     y = 18  (10010)
		//     ===============
		// x ⊙ y = 10  (01010)
		task.xnor(7, 18);
		//     x = 45   (101101)
		//     y = 32   (100000)
		//     ===============
		// x ⊙ y = 50   (110010)
		task.xnor(45, 32);
	}
}

Output

 ( 12 ⊙ 6 ) : 5
 ( 18 ⊙ 7 ) : 10
 ( 45 ⊙ 32 ) : 50
/*
   Swift 4 Program
   Perform XNOR of two numbers
*/
class ExclusiveNOR
{
	// Activate the all bits after most significant bit
	func setAllBit(_ a: Int)->Int
	{
		var n: Int = a;
		n = n | n >> 1;
		n = n | n >> 2;
		n = n | n >> 4;
		n = n | n >> 8;
		n = n | n >> 16;
		return n;
	}
	// Perform xnor operation of given two numbers
	func xnor(_ x: Int, _ y: Int)
	{
		var result: Int = 0;
		if (x > y)
		{
			result = (self.setAllBit(x) ^ x) ^ y;
			print("\n (", x ,"⊙", y ,") : ", result, terminator: "");
		}
		else
		{
			result = (self.setAllBit(y) ^ y) ^ x;
			print("\n (", y ,"⊙", x ,") : ", result, terminator: "");
		}
	}
}
func main()
{
	let task: ExclusiveNOR = ExclusiveNOR();
	//     x = 12  (1100)
	//     y = 6   (0110)
	//     ===============
	// x ⊙ y = 5   (0101)
	task.xnor(12, 6);
	//     x = 7   (00111)
	//     y = 18  (10010)
	//     ===============
	// x ⊙ y = 10  (01010)
	task.xnor(7, 18);
	//     x = 45   (101101)
	//     y = 32   (100000)
	//     ===============
	// x ⊙ y = 50   (110010)
	task.xnor(45, 32);
}
main();

Output

 ( 12 ⊙ 6 ) :  5
 ( 18 ⊙ 7 ) :  10
 ( 45 ⊙ 32 ) :  50
/*
   Kotlin Program
   Perform XNOR of two numbers
*/
class ExclusiveNOR
{
	// Activate the all bits after most significant bit
	fun setAllBit(a: Int): Int
	{
		var n: Int = a;
		n = n or (n shr 1);
		n = n or (n shr 2);
		n = n or (n shr 4);
		n = n or (n shr 8);
		n = n or (n shr 16);
		return n;
	}
	// Perform xnor operation of given two numbers
	fun xnor(x: Int, y: Int): Unit
	{
		var result: Int ;
		if (x > y)
		{
			result = (this.setAllBit(x) xor x) xor y;
			print("\n ( " + x + " ⊙ " + y + " ) : " + result);
		}
		else
		{
			result = (this.setAllBit(y) xor y) xor x;
			print("\n ( " + y + " ⊙ " + x + " ) : " + result);
		}
	}
}
fun main(args: Array <String> ): Unit
{
	var task: ExclusiveNOR = ExclusiveNOR();
	//     x = 12  (1100)
	//     y = 6   (0110)
	//     ===============
	// x ⊙ y = 5   (0101)
	task.xnor(12, 6);
	//     x = 7   (00111)
	//     y = 18  (10010)
	//     ===============
	// x ⊙ y = 10  (01010)
	task.xnor(7, 18);
	//     x = 45   (101101)
	//     y = 32   (100000)
	//     ===============
	// x ⊙ y = 50   (110010)
	task.xnor(45, 32);
}

Output

 ( 12 ⊙ 6 ) : 5
 ( 18 ⊙ 7 ) : 10
 ( 45 ⊙ 32 ) : 50


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