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


XOR, or exclusive OR, is a boolean logic operation which is used in generating parity bits for error checking, fault tolerance, etc. Various symbols are used to represent this operation: ^, ⊕, ⊻, etc.

XOR Logic

The XOR operation is only true if the two arguments are different. This means that the XOR of the same bits is 0, and that of different bits is 1.

Same bits −

0 ^ 0 = 0

1 ^ 1 = 0

Different bits −

0 ^ 1 = 1

1 ^ 0 = 1

Problem Statement

Given two numbers, a and b, find out their XOR after making the length of their binary representations equal.

Hint − The binary representation will become equal by adding trailing zeros to the smaller number.

Examples

Input −

a = 10, b = 5

Output −

0

Explanation

The binary representation of 10 is 1010, and that of 5 is 101.

Adding a trailing zero to 5 makes it 1010.

Hence, the XOR of 1010^1010 = 0.

Thus, the output.

Input

a = 15, b = 8

Output

7

Explanation

The binary representation of 15 is 1111, and that of 8 is 1000.

No trailing zeros are required since the length of both binary representations are equal.

The XOR of 1111 ^ 1000 = 0111, which is 7 in decimal representation. Hence, the output.

Input

a = 15, b = 3

Output

7

Explanation

The binary representation of 15 is 1111. The binary representation of 3 is 11. After adding trailing zeros to the binary representation of 3, it becomes 1100.

The XOR of 1111^1100 = 0011.

0011 is 3 in decimal representation. Hence, the output.

Approach

  • Count the number of bits in both numbers.

  • The number of bits can be counted by right-shifting the number till it becomes zero, and counting the number of times the loop executes. Right shifting a number by 1 is equivalent to dividing it by two.

  • If the number of bits in the smaller number is lesser, then do a left shift as follows: smaller_number << (exceeding bits). It will add trailing zeros to the smaller number equal to the number of exceeding bits. Now the bits in both numbers are equal.

  • XOR both numbers to get the answer and print it.

Pseudo Code

main()
Initialize a -> 15  and  b -> 3.
Function call find_xor(a,b);

find_xor(int a, int b):
c -> minimum of a and b.
d -> maximum of a and b.
count_c -> bit_count(c)
count_d ->bit_count(d)
If count_c < cound_d, then:
c -> c << (count_d - count_c)
Return c XOR d.

bit_count(int x):
count -> 0
while(x != 0):
	Increase the count by one.
	Right shift x by 1, i.e., divide it by 2.
Return x.

Example

Below is a C++ program to calculate the XOR of two numbers after making the length of their binary representation equal.

#include <bits/stdc++.h>
using namespace std;
// Function to count the number of bits in binary representation
// of an integer
int bit_count(int x){
   //Initialize count as zero
   int count = 0;
   //Count the bits till x becomes zero.
   while (x)	{
      //Incrementing the count
	  count++;
      // right shift x by 1
      // i.e, divide by 2
      x = x>>1;
   }
   return count;
}
//Function to find the XOR of two numbers. Trailing zeros are added to the number having a lesser number of bits to make the bits in both numbers equal.
int find_xor(int a, int b){
   //Store the minimum and maximum of both the numbers
   int c = min(a,b);
   int d = max(a,b);
   //Store the number of bits in both numbers.
   int count_c = bit_count(c);
   int count_d = bit_count(d);
   //If the number of bits in c is less, left shift if by the number of exceeding bits.
   if (count_c < count_d){
      c = c << ( count_d - count_c);
   }
   return (c^d);
}
//Driver code
int main(){
   //Initialize a and b.
   int a = 15, b = 3;
   cout << "a = 15, b = 3" << endl;
   //Store the XOR of both the numbers after required computations
   //Function call
   int ans = find_xor(a,b);
   //Print the final result
   cout << "XOR of a and b: "<<ans<<endl;
   return 0;
}

Output

a = 15, b = 3
XOR of a and b: 3

Analysis

Time Complexity − O(log n) [Logarithmic]

The time complexity is logarithmic because of the while loop in the count function.

Since the number is divided by two till it becomes zero, the complexity becomes log n with the base two.

Space Complexity − O(1) [Constant]

The space complexity is constant as no extra space was used in the program.

Conclusion

In this article, we discussed the problem of calculating the XOR of two numbers after making the length of their binary representations equal.

We discussed the concept of XOR and then proceeded with examples and the approach. The approach used trailing zeroes to make the bits of binary representations equal. We also saw the pseudocode and the C++ program for the problem.

Updated on: 16-Aug-2023

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements