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.
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 :
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.
- Check if binary representations of two numbers are anagram
- Comparing leading zeros in binary representations of two numbers
- Check if binary representations of 0 to N are present as substrings in given binary string
- 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
- Find two numbers from their sum and XOR | Set 2
- Count nodes having Bitwise XOR of all edges in their path from the root equal to K
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Rearrange given binary strings to maximize their Bitwise XOR value
- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- 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 even length subarrays having bitwise XOR equal to 0
- 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
- Count all Quadruples from four arrays such that their XOR equals to 'x'
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Minimum Bitwise XOR operations to make any two array elements equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Mithun Kumar