Given two numbers say a and b. Print their XOR after making the lengths of their binary representation equal by adding trailing zeros to the binary representation of smaller one.
Examples :
Input : a = 13, b = 5
Output : 7
Explanation : Binary representation of 13 is 1101 and
of 5 is 101. As the length of "101" is smaller,
so add a '0' to it making it "1010', to make
the length of binary representations equal.
XOR of 1010 and 1101 gives 0111 which is 7.
Input : a = 7, b = 5
Output : 2
Explanation : Since the length of binary representations
of 7 i.e, 111 and 5 i.e, 101 are same, hence simply
print XOR of a and b.
Approach : Count the number of bits in binary representation of smaller number out of a and b. If the number of bits in smaller number(say a) exceeds to that of larger number(say b), then apply left shift to the smaller number by the number of exceeding bits, i.e, a = a<<(exceeding bits). After applying left shift, trailing zeroes will be added at the end of binary representation of smaller number to make the number of bits in binary representation of both the numbers equal. XOR both the binary representations to get the final result.
Below is the implementation of above method :
C++
#include <bits/stdc++.h>
using namespace std;
int count( int n)
{
int c = 0;
while (n)
{
c++;
n = n>>1;
}
return c;
}
int XOR( int a, int b)
{
int c = min(a,b);
int d = max(a,b);
if (count(c) < count(d))
c = c << ( count(d) - count(c) );
return (c^d);
}
int main()
{
int a = 13, b = 5;
cout << XOR(a,b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int count( int n)
{
int c = 0 ;
while (n != 0 )
{
c++;
n = n >> 1 ;
}
return c;
}
static int XOR( int a, int b)
{
int c = Math.min(a, b);
int d = Math.max(a, b);
if (count(c) < count(d))
c = c << ( count(d) - count(c) );
return (c ^ d);
}
public static void main(String args[])
{
int a = 13 , b = 5 ;
System.out.println(XOR(a, b));
}
}
|
Python3
def count(n) :
c = 0
while (n ! = 0 ) :
c + = 1
n = n >> 1
return c
def XOR(a, b) :
c = min (a, b)
d = max (a, b)
if (count(c) < count(d)) :
c = c << ( count(d) - count(c) )
return (c^d)
a = 13 ; b = 5
print (XOR(a, b))
|
C#
using System;
class GFG {
static int count( int n)
{
int c = 0;
while (n != 0)
{
c++;
n = n >> 1;
}
return c;
}
static int XOR( int a, int b)
{
int c = Math.Min(a, b);
int d = Math.Max(a, b);
if (count(c) < count(d))
c = c << ( count(d) - count(c) );
return (c ^ d);
}
public static void Main()
{
int a = 13, b = 5;
Console.WriteLine(XOR(a, b));
}
}
|
PHP
<?php
function count1( $n )
{
$c = 0;
while ( $n )
{
$c ++;
$n = $n >>1;
}
return $c ;
}
function XOR1( $a , $b )
{
$c = min( $a , $b );
$d = max( $a , $b );
if (count1( $c ) < count1( $d ))
$c = $c << ( count1( $d ) - count1( $c ) );
return ( $c ^ $d );
}
$a = 13;
$b = 5;
echo XOR1( $a , $b );
?>
|
Javascript
<script>
function count(n)
{
let c = 0;
while (n != 0)
{
c++;
n = n >> 1;
}
return c;
}
function XOR(a, b)
{
let c = Math.min(a, b);
let d = Math.max(a, b);
if (count(c) < count(d))
c = c << ( count(d) - count(c) );
return (c ^ d);
}
let a = 13, b = 5;
document.write(XOR(a, b));
</script>
|
Output :
7
Time Complexity : O(log2n)
Auxiliary Space : O(1)