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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

7

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Comparing leading zeros in binary representations of two numbers
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Find two numbers from their sum and XOR
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Find XOR of two number without using XOR operator
- Count all Quadruples from four arrays such that their XOR equals to 'x'
- Find number of pairs in an array such that their XOR is 0
- Find a number M < N such that difference between their XOR and AND is maximum
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Minimum Bitwise XOR operations to make any two array elements equal
- Count of elements which are equal to the XOR of the next two elements
- Count numbers whose sum with x is equal to XOR with x
- Count numbers whose difference with N is equal to XOR with N
- Count numbers whose XOR with N is equal to OR with N
- Equal Sum and XOR of three Numbers
- Given a set, find XOR of the XOR's of all subsets.
- Choose X such that (A xor X) + (B xor X) is minimized
- XOR of path between any two nodes in a Binary Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.