Check if two numbers are equal using bitwise operators

Here given code implementation process.

// C Program
// Check if two numbers are equal using bitwise operators
#include <stdio.h>

// Check that given two numbers is equal or not
void isEqual(int a, int b)
{
	// Display given number
	printf("\n Number A : %d", a);
	printf("\n Number B : %d", b);
	if ((a ^ b) != 0)
	{
		// When a and b are not equal
		printf("\n Is Not Same\n");
	}
	else
	{
		//  When a and b are equal
		printf("\n Is Same \n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Cases
	isEqual(1, -1);
	isEqual(-2, -2);
	isEqual(7, 7);
	isEqual(-2, 2);
	return 0;
}

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same
/*
  Java program
  Check if two numbers are equal using bitwise operators
*/
public class Equality
{
	// Check that given two numbers is equal or not
	public void isEqual(int a, int b)
	{
		// Display given number
		System.out.print("\n Number A : " + a);
		System.out.print("\n Number B : " + b);
		if ((a ^ b) != 0)
		{
			// When a and b are not equal
			System.out.print("\n Is Not Same\n");
		}
		else
		{
			//  When a and b are equal
			System.out.print("\n Is Same \n");
		}
	}
	public static void main(String[] args)
	{
		Equality task = new Equality();
		// Test Cases
		task.isEqual(1, -1);
		task.isEqual(-2, -2);
		task.isEqual(7, 7);
		task.isEqual(-2, 2);
	}
}

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same
// Include header file
#include <iostream>
using namespace std;

/*
  C++ program
  Check if two numbers are equal using bitwise operators
*/

class Equality
{
	public:
		// Check that given two numbers is equal or not
		void isEqual(int a, int b)
		{
			// Display given number
			cout << "\n Number A : " << a;
			cout << "\n Number B : " << b;
			if ((a ^ b) != 0)
			{
				// When a and b are not equal
				cout << "\n Is Not Same\n";
			}
			else
			{
				//  When a and b are equal
				cout << "\n Is Same \n";
			}
		}
};
int main()
{
	Equality task = Equality();
	// Test Cases
	task.isEqual(1, -1);
	task.isEqual(-2, -2);
	task.isEqual(7, 7);
	task.isEqual(-2, 2);
	return 0;
}

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same
// Include namespace system
using System;
/*
  C# program
  Check if two numbers are equal using bitwise operators
*/
public class Equality
{
	// Check that given two numbers is equal or not
	public void isEqual(int a, int b)
	{
		// Display given number
		Console.Write("\n Number A : " + a);
		Console.Write("\n Number B : " + b);
		if ((a ^ b) != 0)
		{
			// When a and b are not equal
			Console.Write("\n Is Not Same\n");
		}
		else
		{
			//  When a and b are equal
			Console.Write("\n Is Same \n");
		}
	}
	public static void Main(String[] args)
	{
		Equality task = new Equality();
		// Test Cases
		task.isEqual(1, -1);
		task.isEqual(-2, -2);
		task.isEqual(7, 7);
		task.isEqual(-2, 2);
	}
}

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same
<?php
/*
  Php program
  Check if two numbers are equal using bitwise operators
*/
class Equality
{
	// Check that given two numbers is equal or not
	public	function isEqual($a, $b)
	{
		// Display given number
		echo "\n Number A : ". $a;
		echo "\n Number B : ". $b;
		if (($a ^ $b) != 0)
		{
			// When a and b are not equal
			echo "\n Is Not Same\n";
		}
		else
		{
			//  When a and b are equal
			echo "\n Is Same \n";
		}
	}
}

function main()
{
	$task = new Equality();
	// Test Cases
	$task->isEqual(1, -1);
	$task->isEqual(-2, -2);
	$task->isEqual(7, 7);
	$task->isEqual(-2, 2);
}
main();

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same
/*
  Node Js program
  Check if two numbers are equal using bitwise operators
*/
class Equality
{
	// Check that given two numbers is equal or not
	isEqual(a, b)
	{
		// Display given number
		process.stdout.write("\n Number A : " + a);
		process.stdout.write("\n Number B : " + b);
		if ((a ^ b) != 0)
		{
			// When a and b are not equal
			process.stdout.write("\n Is Not Same\n");
		}
		else
		{
			//  When a and b are equal
			process.stdout.write("\n Is Same \n");
		}
	}
}

function main()
{
	var task = new Equality();
	// Test Cases
	task.isEqual(1, -1);
	task.isEqual(-2, -2);
	task.isEqual(7, 7);
	task.isEqual(-2, 2);
}
main();

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same
#   Python 3 program
#   Check if two numbers are equal using bitwise operators

class Equality :
	#  Check that given two numbers is equal or not
	def isEqual(self, a, b) :
		#  Display given number
		print("\n Number A : ", a, end = "")
		print("\n Number B : ", b, end = "")
		if ((a ^ b) != 0) :
			#  When a and b are not equal
			print("\n Is Not Same")
		else :
			#   When a and b are equal
			print("\n Is Same ")
		
	

def main() :
	task = Equality()
	#  Test Cases
	task.isEqual(1, -1)
	task.isEqual(-2, -2)
	task.isEqual(7, 7)
	task.isEqual(-2, 2)

if __name__ == "__main__": main()

Output

 Number A :  1
 Number B :  -1
 Is Not Same

 Number A :  -2
 Number B :  -2
 Is Same

 Number A :  7
 Number B :  7
 Is Same

 Number A :  -2
 Number B :  2
 Is Not Same
#   Ruby program
#   Check if two numbers are equal using bitwise operators

class Equality 
	#  Check that given two numbers is equal or not
	def isEqual(a, b) 
		#  Display given number
		print("\n Number A : ", a)
		print("\n Number B : ", b)
		if ((a ^ b) != 0) 
			#  When a and b are not equal
			print("\n Is Not Same\n")
		else 
			#   When a and b are equal
			print("\n Is Same \n")
		end

	end

end

def main() 
	task = Equality.new()
	#  Test Cases
	task.isEqual(1, -1)
	task.isEqual(-2, -2)
	task.isEqual(7, 7)
	task.isEqual(-2, 2)
end

main()

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same 

 Number A : 7
 Number B : 7
 Is Same 

 Number A : -2
 Number B : 2
 Is Not Same
/*
  Scala program
  Check if two numbers are equal using bitwise operators
*/
class Equality
{
	// Check that given two numbers is equal or not
	def isEqual(a: Int, b: Int): Unit = {
		// Display given number
		print("\n Number A : " + a);
		print("\n Number B : " + b);
		if ((a ^ b) != 0)
		{
			// When a and b are not equal
			print("\n Is Not Same\n");
		}
		else
		{
			//  When a and b are equal
			print("\n Is Same \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Equality = new Equality();
		// Test Cases
		task.isEqual(1, -1);
		task.isEqual(-2, -2);
		task.isEqual(7, 7);
		task.isEqual(-2, 2);
	}
}

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same
/*
  Swift 4 program
  Check if two numbers are equal using bitwise operators
*/
class Equality
{
	// Check that given two numbers is equal or not
	func isEqual(_ a: Int, _ b: Int)
	{
		// Display given number
		print("\n Number A : ", a, terminator: "");
		print("\n Number B : ", b, terminator: "");
		if ((a ^ b)  != 0)
		{
			// When a and b are not equal
			print("\n Is Not Same");
		}
		else
		{
			//  When a and b are equal
			print("\n Is Same ");
		}
	}
}
func main()
{
	let task: Equality = Equality();
	// Test Cases
	task.isEqual(1, -1);
	task.isEqual(-2, -2);
	task.isEqual(7, 7);
	task.isEqual(-2, 2);
}
main();

Output

 Number A :  1
 Number B :  -1
 Is Not Same

 Number A :  -2
 Number B :  -2
 Is Same

 Number A :  7
 Number B :  7
 Is Same

 Number A :  -2
 Number B :  2
 Is Not Same
/*
  Kotlin program
  Check if two numbers are equal using bitwise operators
*/
class Equality
{
	// Check that given two numbers is equal or not
	fun isEqual(a: Int, b: Int): Unit
	{
		// Display given number
		print("\n Number A : " + a);
		print("\n Number B : " + b);
		if ((a xor b) != 0)
		{
			// When a and b are not equal
			print("\n Is Not Same\n");
		}
		else
		{
			//  When a and b are equal
			print("\n Is Same \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Equality = Equality();
	// Test Cases
	task.isEqual(1, -1);
	task.isEqual(-2, -2);
	task.isEqual(7, 7);
	task.isEqual(-2, 2);
}

Output

 Number A : 1
 Number B : -1
 Is Not Same

 Number A : -2
 Number B : -2
 Is Same

 Number A : 7
 Number B : 7
 Is Same

 Number A : -2
 Number B : 2
 Is Not Same


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