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: The first line of the input contains integer T denoting the number of test cases. For each test case, there are two integer inputs a & b. Output: For each test case, the output is the integer displaying the XOR of a & b after making them of equal lengths. Constraints: 1<=T<=100 1<=a,b<=107 Example: Input: 4 2 6 3 10 5 24 1 20 Output: 2 6 12 4 Explanation: 1. The binary representation of 2 is 10 and of 6 is 110. As the length of "10" is smaller, so add a '0' to it making it "100', to make the length of binary representations equal. XOR of 100 and 110 gives 010 which is 2. Below is my code
#include
#include
#include
#include
#include
using namespace std;int main()
{
int cases, binary[100];
cin>>cases;int \*result = new int\[cases\]; int j; for(j=0; j\>n; int temp=n, i=0, len; while(temp > 0){ binary\[i\] = temp%2; temp /= 2; i++; } len = i; string str ; for(int i=0; i str2.length()) ? str1.length() : str2.length() ; if(str1.length() < (unsigned)len){ int diff = len - str1.length(); for(int i=0; i\=0; ++i,--m){ if(str3\[i\] == '1'){ val += pow(2, m); } } result\[j\] = val; } for(int i=0; i
how to make it more efficient & short(i haven't studied oops yet)
-
Input: The first line of the input contains integer T denoting the number of test cases. For each test case, there are two integer inputs a & b. Output: For each test case, the output is the integer displaying the XOR of a & b after making them of equal lengths. Constraints: 1<=T<=100 1<=a,b<=107 Example: Input: 4 2 6 3 10 5 24 1 20 Output: 2 6 12 4 Explanation: 1. The binary representation of 2 is 10 and of 6 is 110. As the length of "10" is smaller, so add a '0' to it making it "100', to make the length of binary representations equal. XOR of 100 and 110 gives 010 which is 2. Below is my code
#include
#include
#include
#include
#include
using namespace std;int main()
{
int cases, binary[100];
cin>>cases;int \*result = new int\[cases\]; int j; for(j=0; j\>n; int temp=n, i=0, len; while(temp > 0){ binary\[i\] = temp%2; temp /= 2; i++; } len = i; string str ; for(int i=0; i str2.length()) ? str1.length() : str2.length() ; if(str1.length() < (unsigned)len){ int diff = len - str1.length(); for(int i=0; i\=0; ++i,--m){ if(str3\[i\] == '1'){ val += pow(2, m); } } result\[j\] = val; } for(int i=0; i
how to make it more efficient & short(i haven't studied oops yet)
You can get the length of each value (remember to save the originals) by repeatedly shifting one bit right until it is zero. You then shift the smaller value left by the difference between their lengths. That will give you two integers of the same length. All you need then is to create the XOR of the two and print it out.
-
Input: The first line of the input contains integer T denoting the number of test cases. For each test case, there are two integer inputs a & b. Output: For each test case, the output is the integer displaying the XOR of a & b after making them of equal lengths. Constraints: 1<=T<=100 1<=a,b<=107 Example: Input: 4 2 6 3 10 5 24 1 20 Output: 2 6 12 4 Explanation: 1. The binary representation of 2 is 10 and of 6 is 110. As the length of "10" is smaller, so add a '0' to it making it "100', to make the length of binary representations equal. XOR of 100 and 110 gives 010 which is 2. Below is my code
#include
#include
#include
#include
#include
using namespace std;int main()
{
int cases, binary[100];
cin>>cases;int \*result = new int\[cases\]; int j; for(j=0; j\>n; int temp=n, i=0, len; while(temp > 0){ binary\[i\] = temp%2; temp /= 2; i++; } len = i; string str ; for(int i=0; i str2.length()) ? str1.length() : str2.length() ; if(str1.length() < (unsigned)len){ int diff = len - str1.length(); for(int i=0; i\=0; ++i,--m){ if(str3\[i\] == '1'){ val += pow(2, m); } } result\[j\] = val; } for(int i=0; i
how to make it more efficient & short(i haven't studied oops yet)
Using Richard's suggestion:
unsigned int myxor(unsigned int a, unsigned int b)
{
if ( a < b) swap(a,b);// now a >= b
int lab = 0; // difference of a and b binary length
int ca = a;
int cb = b;
while ( ca )
{
if ( cb == 0 )
++lab;
ca >>= 1;
cb >>= 1;
}
return ( a ^ (b << lab));
} -
Using Richard's suggestion:
unsigned int myxor(unsigned int a, unsigned int b)
{
if ( a < b) swap(a,b);// now a >= b
int lab = 0; // difference of a and b binary length
int ca = a;
int cb = b;
while ( ca )
{
if ( cb == 0 )
++lab;
ca >>= 1;
cb >>= 1;
}
return ( a ^ (b << lab));
}