Open In App

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

Last Updated : 03 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 integer
int 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 same
import 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 integer
def 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 Code
a = 13; b = 5
print(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 same
using 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 integer
function 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)



Previous Article
Next Article

Similar Reads

Maximum XOR of Two Binary Numbers after shuffling their bits.
Given two n-bit integers a and b, find the maximum possible value of (a' xor b') where a' is a shuffle-a, b' is a shuffle-b and xor is the bitwise xor operator. Note: Consider a n-bit integer a. We call an integer a' as shuffle-a if a' can be obtained by shuffling the bits of a in its binary representation. For eg. if n = 5 and a = 6 = (00110)2, a'
8 min read
Maximize difference between odd and even indexed array elements by swapping unequal adjacent bits in their binary representations
Given an array arr[] consisting of N positive integers, the task is to find the maximum absolute difference between the sum of the array elements placed at even and odd indices of the array by swapping unequal adjacent bits of in the binary representation of any array element any number of times. Examples: Input: arr[] = {5, 7, 3, 1, 8}Output: 9Exp
12 min read
Maximize difference between odd and even-indexed array elements by rotating their binary representations
Given an array arr[] consisting of N positive integers, the task is to find the maximum absolute difference between the sum of the array elements placed at even indices and those at odd indices of the array by rotating their binary representations any number of times. Consider only 8-bit representation. Examples: Input: arr[] = {123, 86, 234, 189}O
12 min read
Bitwise XOR of same indexed array elements after rearranging an array to make XOR of same indexed elements of two arrays equal
Given two arrays A[] and B[] consisting of N positive integers, the task is to the Bitwise XOR of same indexed array elements after rearranging the array B[] such that the Bitwise XOR of the same indexed elements of the arrays A[] becomes equal. Examples: Input: A[] = {1, 2, 3}, B[] = {4, 6, 7}Output: 5Explanation:Below are the possible arrangement
14 min read
Comparing leading zeros in binary representations of two numbers
Given two integer numbers x and y. Compare and print which one of them has more leading zeros using Bitwise operation. If both the no. have the same no. of leading zeros, print "Equal".Note:- A leading zero is any 0 digit that comes before the first nonzero digit in the binary notation of the number.Examples: Input : 10, 16Output :10Explanation: If
8 min read
Check if binary representations of two numbers are anagram
Given two numbers you are required to check whether they are anagrams of each other or not in binary representation.Examples: Input : a = 8, b = 4 Output : Yes Binary representations of both numbers have same 0s and 1s. Input : a = 4, b = 5 Output : No Simple Approach: Find the Binary Representation of 'a' and 'b' using a simple decimal-to-binary r
9 min read
Minimum flips in two binary arrays so that their XOR is equal to another array
Given three binary arrays, each of size n, the task is to find minimum flip of bits in the first and second array such that the XOR of i'th index bit of first and second arrays is equal to i'th index bit of third array. Given a constraint that we can only flip at most p bits of array 1 and at most q bits of array 2. If not possible, output -1. Rear
8 min read
Count of subarrays in range [L, R] having XOR + 1 equal to XOR (XOR) 1 for M queries
Given an array, arr[] of N positive integers and M queries which consist of two integers [Li, Ri] where 1 ? Li ? Ri ? N. For each query, find the number of subarrays in range [Li, Ri] for which (X+1)=(X?1) where X denotes the xor of a subarray. Input: arr[]= {1, 2, 9, 8, 7}, queries[] = {{1, 5}, {3, 4}}Output: 6 1Explanation: Query 1: L=1, R=5: sub
12 min read
Check if binary representations of 0 to N are present as substrings in given binary string
Give binary string str and an integer N, the task is to check if the substrings of the string contain all binary representations of non-negative integers less than or equal to the given integer N. Examples: Input: str = “0110", N = 3 Output: True Explanation: Since substrings “0", “1", “10", and “11" can be formed from given string. Hence all binar
9 min read
Modify array by removing characters from their Hexadecimal representations which are present in a given string
Given an array arr[] of size N and a string S, the task is to modify given array by removing all characters from their hexadecimal representations that are present in S and then replacing the equivalent decimal element back into the array. Examples: Input: arr[] = {74, 91, 31, 122}, S = "1AB"Output: {4, 5, 15, 7}Explanation: 74 -&gt; (4A)16 -&gt; (
10 min read
Article Tags :
Practice Tags :