Click here to Skip to main content
15,903,175 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: MSXML load - not able to read chinese characters in path Pin
Gopi Nath28-Feb-18 23:12
Gopi Nath28-Feb-18 23:12 
GeneralRe: MSXML load - not able to read chinese characters in path Pin
Richard MacCutchan28-Feb-18 23:15
mveRichard MacCutchan28-Feb-18 23:15 
GeneralRe: MSXML load - not able to read chinese characters in path Pin
Gopi Nath28-Feb-18 23:46
Gopi Nath28-Feb-18 23:46 
QuestionRe: MSXML load - not able to read chinese characters in path Pin
David Crow1-Mar-18 7:26
David Crow1-Mar-18 7:26 
AnswerRe: MSXML load - not able to read chinese characters in path Pin
Victor Nijegorodov1-Mar-18 10:24
Victor Nijegorodov1-Mar-18 10:24 
QuestionRe: MSXML load - not able to read chinese characters in path Pin
Randor 1-Mar-18 14:46
professional Randor 1-Mar-18 14:46 
AnswerRe: MSXML load - not able to read chinese characters in path Pin
Gopi Nath19-Mar-18 23:18
Gopi Nath19-Mar-18 23:18 
QuestionGiven 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. Pin
Tarun Jha25-Feb-18 10:14
Tarun Jha25-Feb-18 10:14 
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 <iostream>
#include <cmath>
#include <sstream>
#include <cstring>
#include <string>
using namespace std;

int main()
{
	int cases, binary[100];
	cin>>cases;

	int *result = new int[cases];
	int j;
	for(j=0; j<cases; j++){
		
        stringstream ss[3];
		for(int k=0; k<2; k++)
		{
			int n;
			cin>>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<len; ++i){
				stringstream ss;
				ss << binary[(len-1)-i];
				str += ss.str() ;
			}
			ss[k] << str;
		}

    	string str1 = ss[0].str(), str2 = ss[1].str();
	    int len = (str1.length() > str2.length()) ? str1.length() : str2.length() ;

    	if(str1.length() < (unsigned)len){
       	 int diff = len - str1.length();
       	 for(int i=0; i<diff; ++i){
        	    str1 += '0';
    	    }
   	 	}
    	else if(str2.length() < (unsigned)len){
      	  int diff = len - str2.length();
       	 for(int i=0; i<diff; ++i){
        	    str2 += '0';
    	    }
   	 	}

   	 	string str3 = str1;
  	  	for(int i=0; i<len; ++i){
   	    	 if(str1[i]==str2[i]){
    	        str3[i]='0';
    	    }else   str3[i]='1';
    	}
		int length = str3.length(), val=0;
 	   	for(int i=0, m=length-1; i<length, m>=0; ++i,--m){
       		if(str3[i] == '1'){
	        	    val += pow(2, m);
    	   	 }
   	 	}
   	 	result[j] = val;
	}

	for(int i=0; i<cases; i++){
		cout<<result[i]<<endl;
	}
	return 0;
}


how to make it more efficient & short(i haven't studied oops yet)
AnswerRe: 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. Pin
Richard MacCutchan25-Feb-18 21:54
mveRichard MacCutchan25-Feb-18 21:54 
AnswerRe: 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. Pin
CPallini25-Feb-18 23:17
mveCPallini25-Feb-18 23:17 
GeneralRe: 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. Pin
Richard MacCutchan25-Feb-18 23:32
mveRichard MacCutchan25-Feb-18 23:32 
GeneralRe: 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. Pin
CPallini25-Feb-18 23:53
mveCPallini25-Feb-18 23:53 
QuestionStrange Debugging results Pin
ForNow24-Feb-18 16:36
ForNow24-Feb-18 16:36 
GeneralRe: Strange Debugging results Pin
Richard MacCutchan24-Feb-18 20:13
mveRichard MacCutchan24-Feb-18 20:13 
GeneralRe: Strange Debugging results Pin
ForNow24-Feb-18 20:38
ForNow24-Feb-18 20:38 
GeneralRe: Strange Debugging results Pin
Richard MacCutchan25-Feb-18 3:43
mveRichard MacCutchan25-Feb-18 3:43 
GeneralRe: Strange Debugging results Pin
ForNow25-Feb-18 5:15
ForNow25-Feb-18 5:15 
GeneralRe: Strange Debugging results Pin
Richard MacCutchan25-Feb-18 6:20
mveRichard MacCutchan25-Feb-18 6:20 
GeneralRe: Strange Debugging results Pin
ForNow25-Feb-18 6:56
ForNow25-Feb-18 6:56 
GeneralRe: Strange Debugging results Pin
ForNow25-Feb-18 8:28
ForNow25-Feb-18 8:28 
GeneralRe: Strange Debugging results Pin
Richard MacCutchan25-Feb-18 21:36
mveRichard MacCutchan25-Feb-18 21:36 
GeneralRe: Strange Debugging results Pin
ForNow26-Feb-18 1:53
ForNow26-Feb-18 1:53 
QuestionWhen an process is loaded Pin
_Flaviu22-Feb-18 23:15
_Flaviu22-Feb-18 23:15 
QuestionRe: When an process is loaded Pin
Richard MacCutchan22-Feb-18 23:18
mveRichard MacCutchan22-Feb-18 23:18 
AnswerRe: When an process is loaded Pin
_Flaviu22-Feb-18 23:35
_Flaviu22-Feb-18 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.