Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 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.

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.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    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)

    L CPalliniC 2 Replies Last reply
    0
    • T Tarun Jha

      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)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Tarun Jha

        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)

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #3

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

        In testa che avete, signor di Ceprano?

        L 1 Reply Last reply
        0
        • CPalliniC CPallini

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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Better than my idea. :thumbsup:

          CPalliniC 1 Reply Last reply
          0
          • L Lost User

            Better than my idea. :thumbsup:

            CPalliniC Online
            CPalliniC Online
            CPallini
            wrote on last edited by
            #5

            Based on your idea.

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups