Check if two numbers are equal without using arithmetic and comparison operators
Given two numbers, the task is to check if two numbers are equal without using Arithmetic and Comparison Operators or String functions.
Method 1 : The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero.
C++
#include <iostream>
using namespace std;
void areSame( int a, int b)
{
if (a ^ b)
cout << "Not Same" ;
else
cout << "Same" ;
}
int main()
{
areSame(10, 20);
}
|
Java
class GFG {
static void areSame( int a, int b)
{
if ((a ^ b) != 0 )
System.out.print( "Not Same" );
else
System.out.print( "Same" );
}
public static void main(String[] args)
{
areSame( 10 , 20 );
}
}
|
Python3
def areSame(a, b):
if ((a ^ b) ! = 0 ):
print ( "Not Same" )
else :
print ( "Same" )
areSame( 10 , 20 )
|
C#
using System;
class GFG {
static void areSame( int a, int b)
{
if ((a ^ b) != 0)
Console.Write( "Not Same" );
else
Console.Write( "Same" );
}
public static void Main(String[] args)
{
areSame(10, 20);
}
}
|
Javascript
<script>
function areSame(a, b)
{
if ((a ^ b) != 0)
document.write( "Not Same" );
else
document.write( "Same" );
}
areSame(10, 20);
</script>
|
PHP
<?php
function areSame( $a , $b )
{
if ( $a ^ $b )
echo "Not Same" ;
else
echo "Same" ;
}
areSame(10, 20);
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2 : Here idea is using complement ( ~ ) and bit-wise ‘&’ operator.
C++
#include <iostream>
using namespace std;
void areSame( int a, int b)
{
if ((a & ~b) == 0)
cout << "Same" ;
else
cout << "Not Same" ;
}
int main()
{
areSame(10, 20);
}
|
Java
class GFG {
static void areSame( int a, int b)
{
if ((a & ~b) == 0 && (~a & b) == 0 )
System.out.print( "Same" );
else
System.out.print( "Not Same" );
}
public static void main(String args[])
{
areSame( 10 , 20 );
}
}
|
Python3
def areSame(a, b):
if ((a & ~b) = = 0 and (~a & b) = = 0 ):
print ( "Same" )
else :
print ( "Not Same" )
areSame( 10 , 20 )
|
C#
using System;
class GFG {
static void areSame( int a, int b)
{
if ((a & ~b) == 0 && (~a & b) == 0)
Console.Write( "Same" );
else
Console.Write( "Not Same" );
}
public static void Main()
{
areSame(10, 20);
}
}
|
Javascript
<script>
function areSame(a, b)
{
if ((a & ~b) == 0 && (~a & b) == 0)
document.write( "Same" );
else
document.write( "Not Same" );
}
areSame(10, 20);
</script>
|
PHP
<?php
function areSame( $a , $b )
{
if (( $a & ~ $b )==0 && (~ $a & $b )==0)
echo "Same" ;
else
echo "Not Same" ;
}
areSame(10, 20);
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Using bit manipulation:
Approach:
Another approach is to use bit manipulation to compare each bit of the two numbers. We can use the bit-shift operators to extract each bit and compare them one by one.
- Define a function named is_equal that takes two arguments num1 and num2.
- Initialize a variable mask to 1.
- Loop through the range of 32 bits (assuming 32-bit integers).
- Use the bitwise AND operator (&) to extract the i-th bit of num1 and num2.
- Compare the extracted bits using the not equal to operator (!=).
- If the extracted bits are not equal, return False.
- Shift the mask left by one bit using the left shift operator (<<).
- Return True if all bits are equal.
C++
#include <iostream>
using namespace std;
bool isEqual( int num1, int num2)
{
int mask = 1;
for ( int i = 0; i < 32;
i++) {
if ((num1 & mask) != (num2 & mask)) {
return false ;
}
mask <<= 1;
}
return true ;
}
int main()
{
cout << (isEqual(10, 10) ? "True" : "False" ) << endl;
cout << (isEqual(10, 20) ? "True" : "False" ) << endl;
return 0;
}
|
Java
public class GFG {
static boolean isEqual( int num1, int num2)
{
int mask = 1 ;
for ( int i = 0 ; i < 32 ;
i++) {
if ((num1 & mask) != (num2 & mask)) {
return false ;
}
mask <<= 1 ;
}
return true ;
}
public static void main(String[] args)
{
System.out.println(isEqual( 10 , 10 )
? "True"
: "False" );
System.out.println(isEqual( 10 , 20 )
? "True"
: "False" );
}
}
|
Python3
def is_equal(num1, num2):
mask = 1
for i in range ( 32 ):
if (num1 & mask) ! = (num2 & mask):
return False
mask << = 1
return True
print (is_equal( 10 , 10 ))
print (is_equal( 10 , 20 ))
|
C#
using System;
class Program
{
static bool IsEqual( int num1, int num2)
{
int mask = 1;
for ( int i = 0; i < 32; i++)
{
if ((num1 & mask) != (num2 & mask))
{
return false ;
}
mask <<= 1;
}
return true ;
}
static void Main()
{
Console.WriteLine(IsEqual(10, 10) ? "True" : "False" );
Console.WriteLine(IsEqual(10, 20) ? "True" : "False" );
}
}
|
Javascript
function isEqual(num1, num2) {
let mask = 1;
for (let i = 0; i < 32; i++) {
if ((num1 & mask) !== (num2 & mask)) {
return false ;
}
mask <<= 1;
}
return true ;
}
console.log(isEqual(10, 10) ? "True" : "False" );
console.log(isEqual(10, 20) ? "True" : "False" );
|
Time complexity: O(log n)
Space complexity: O(1)
Source: https://www.geeksforgeeks.org/count-of-n-digit-numbers-whose-sum-of-digits-equals-to-given-sum/
Last Updated :
04 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...