XOR of two numbers after making length of their binary representations equal

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++
// C++ implementation to return // XOR of two numbers after making// length of their binary representation same#include <bits/stdc++.h>using namespace std;Â
// function to count the number // of bits in binary representation // of an integerint count(int n){    // initialize count    int c = 0;         // count till n is non zero    while (n)    {        c++;                 // right shift by 1         // i.e, divide by 2        n = n>>1;    }    return c;}Â
// function to calculate the xor of// two numbers by adding trailing // zeros to the number having less number // of bits in its binary representation.int XOR(int a, int b){    // stores the minimum and maximum     int c = min(a,b);    int d = max(a,b);         // left shift if the number of bits    // are less in binary representation    if (count(c) < count(d))       c = c << ( count(d) - count(c) );          return (c^d); }Â
// driver code to check the above function int main(){Â Â Â Â Â Â int a = 13, b = 5;Â Â Â Â cout << XOR(a,b);Â Â Â Â Â Â Â return 0;} |
Java
// Java implementation to return // XOR of two numbers after making// length of their binary representation sameimport java.io.*;Â
class GFG {         // function to count the number     // of bits in binary representation     // of an integer    static int count(int n)    {        // initialize count        int c = 0;                 // count till n is non zero        while (n != 0)        {            c++;                         // right shift by 1             // i.e, divide by 2            n = n >> 1;        }        return c;    }         // function to calculate the xor of    // two numbers by adding trailing     // zeros to the number having less number     // of bits in its binary representation.    static int XOR(int a, int b)    {        // stores the minimum and maximum         int c = Math.min(a, b);        int d = Math.max(a, b);                 // left shift if the number of bits        // are less in binary representation        if (count(c) < count(d))        c = c << ( count(d) - count(c) );                  return (c ^ d);     }         // driver code to check the above function     public static void main(String args[])    {         int a = 13, b = 5;        System.out.println(XOR(a, b));    }}Â
// This code is contributed by Nikita Tiwari. |
Python3
# Python 3 implementation to return XOR# of two numbers after making length# of their binary representation sameÂ
# Function to count the number of bits# in binary representation of an integerdef count(n) :         # initialize count    c = 0         # count till n is non zero    while (n != 0) :        c += 1                 # right shift by 1         # i.e, divide by 2        n = n >> 1             return c     # Function to calculate the xor of# two numbers by adding trailing # zeros to the number having less number # of bits in its binary representation.def XOR(a, b) :         # stores the minimum and maximum     c = min(a, b)    d = max(a, b)         # left shift if the number of bits    # are less in binary representation    if (count(c) < count(d)) :        c = c << ( count(d) - count(c) )          return (c^d)Â
# Driver Codea = 13; b = 5print(XOR(a, b))Â
Â
# This code is contributed by Nikita Tiwari. |
C#
// C# implementation to return XOR of two // numbers after making length of their // binary representation sameusing System;Â
class GFG {         // function to count the number     // of bits in binary representation     // of an integer    static int count(int n)    {                 // initialize count        int c = 0;                 // count till n is non zero        while (n != 0)        {                         c++;                         // right shift by 1             // i.e, divide by 2            n = n >> 1;        }                 return c;    }         // function to calculate the xor of    // two numbers by adding trailing     // zeros to the number having less number     // of bits in its binary representation.    static int XOR(int a, int b)    {                 // stores the minimum and maximum         int c = Math.Min(a, b);        int d = Math.Max(a, b);                 // left shift if the number of bits        // are less in binary representation        if (count(c) < count(d))        c = c << ( count(d) - count(c) );                  return (c ^ d);     }         // driver code to check the above function     public static void Main()    {         int a = 13, b = 5;                 Console.WriteLine(XOR(a, b));    }}Â
// This code is contributed by vt_m. |
PHP
<?php// php implementation to return XOR// of two numbers after making// length of their binary // representation sameÂ
// function to count the number // of bits in binary representation // of an integerfunction count1($n){         // initialize count    $c = 0;         // count till n is    // non zero    while ($n)    {        $c++;                 // right shift by 1         // i.e, divide by 2        $n = $n>>1;    }    return $c;}Â
// function to calculate the xor of// two numbers by adding trailing // zeros to the number having less number // of bits in its binary representation.function XOR1($a, $b){         // stores the minimum     // and maximum     $c = min($a,$b);    $d = max($a,$b);         // left shift if the number of bits    // are less in binary representation    if (count1($c) < count1($d))    $c = $c << ( count1($d) - count1($c) );          return ($c^$d); }Â
    // Driver Code     $a = 13;    $b = 5;    echo XOR1($a, $b); Â
// This code is contributed by mits ?> |
Javascript
<script>Â
// JavaScript program to return // XOR of two numbers after making// length of their binary representation sameÂ
    // function to count the number     // of bits in binary representation     // of an integer    function count(n)    {        // initialize count        let c = 0;                   // count till n is non zero        while (n != 0)        {            c++;                           // right shift by 1             // i.e, divide by 2            n = n >> 1;        }        return c;    }           // function to calculate the xor of    // two numbers by adding trailing     // zeros to the number having less number     // of bits in its binary representation.    function XOR(a, b)    {        // stores the minimum and maximum         let c = Math.min(a, b);        let d = Math.max(a, b);                   // left shift if the number of bits        // are less in binary representation        if (count(c) < count(d))        c = c << ( count(d) - count(c) );                    return (c ^ d);     }  // Driver codeÂ
        let a = 13, b = 5;        document.write(XOR(a, b));Â
</script> |
Output :Â
7
 Time Complexity : O(log2n)
Auxiliary Space : O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



