Skip to main content

Check if two numbers are equal using bitwise operators

The given problem is to check whether two numbers are equal using bitwise operators. Bitwise operators manipulate individual bits of numbers. The goal is to determine if two numbers have the same binary representation, indicating they are equal. The primary bitwise operator used for this purpose is the XOR (^) operator.

Problem Statement

We are given two integer numbers, 'a' and 'b.' The task is to determine whether they are equal or not using bitwise operators. If 'a' and 'b' have the same binary representation, then they are equal; otherwise, they are not.

Explanation with Example

Let's understand the concept with an example. Consider two numbers '5' and '7':

Binary representation of 5: 00000101 Binary representation of 7: 00000111

Now, let's use the XOR operator on these numbers:

 00000101
^00000111
----------
 00000010

The result of the XOR operation is '00000010,' which is the binary representation of the number 2. Since the XOR result is not zero, '5' and '7' are not equal.

Pseudocode

Here is the pseudocode for the given problem:

isEqual(a, b):
    result = a XOR b
    if result is 0:
        print "Is Same"
    else:
        print "Is Not Same"

Algorithm

  1. Start the isEqual function, which takes two integer inputs 'a' and 'b.'
  2. Calculate the result of 'a' XOR 'b' and store it in a variable called 'result.'
  3. If 'result' is equal to 0, print "Is Same," indicating that 'a' and 'b' are equal. Otherwise, print "Is Not Same."

Explanation of the Algorithm

In the algorithm, we use the XOR operator to obtain the bitwise difference between 'a' and 'b.' If the two numbers are equal, their binary representations will have no difference, and the XOR result will be zero. Otherwise, the XOR result will have set bits at the positions where 'a' and 'b' differ.

Resultant Output Explanation

Now, let's analyze the output of the given code:

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

The binary representation of 1 is 00000001, and the binary representation of -1 (in 2's complement) is 11111111. When we perform the XOR operation between these numbers, we get 11111110, which is -2 in decimal. Since the XOR result is not zero, the code correctly outputs "Is Not Same."

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

The binary representation of -2 is 11111110. When we perform the XOR operation of -2 with itself, the result is zero (00000000). Therefore, the code outputs "Is Same," which is the correct output.

Number A : 7
Number B : 7
Is Same

The binary representation of 7 is 00000111. When we perform the XOR operation of 7 with itself, the result is zero (00000000). Therefore, the code outputs "Is Same," which is the correct output.

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

The binary representation of -2 is 11111110, and the binary representation of 2 is 00000010. When we perform the XOR operation between these numbers, we get 11111100, which is -4 in decimal. Since the XOR result is not zero, the code correctly outputs "Is Not Same."

Code Solution

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

Time Complexity

The time complexity of the given code is constant O(1) because the operations performed are independent of the size of the input numbers. The XOR operation takes constant time regardless of the number of bits in 'a' and 'b.' Therefore, the code's time complexity remains constant.





Comment

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