xor function

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1071 Views - Last Post: 24 March 2013 - 05:02 PM Rate Topic: -----

#16 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • Joined: 08-August 08

Re: xor function

Posted 22 March 2013 - 11:36 AM

View Postchenfire, on 22 March 2013 - 12:18 PM, said:

you didn't look at evryting I wrote right? :P/>

Wrong. Your call from main() would need to be in a loop because your function is not recursive.
Was This Post Helpful? 0
  • +
  • -

#17 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 23 March 2013 - 10:12 AM

well, here is my view so far:
lets say that these are the inputs:
n1[]={1,0,1,1}
n2[]={1,1,0,1}
length=4

so we send to the function:(n1,n2,length-1).

f(3)=0+f(2)
f(2)=2+f(1)
f(1)=2*2 +f(0)
f(0)=0+f(-1)
f(-1)=0

we will stop when length==-1, because if we will stop at 0,we will "loose" f(0), that might be not zero.

but without static variables, how can i do it?

thanks :)
Was This Post Helpful? 0
  • +
  • -

#18 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 24 March 2013 - 09:32 AM

well i think that I have solved it:



int binaryXor(int firstnumber[],int secondnumber[],int length){
	if(length==-1) return 0;
	if(firstnumber[length]!=secondnumber[length])
		return 1+2*binaryXor(firstnumber,secondnumber,length-1);
	return 2*binaryXor(firstnumber,secondnumber,length-1);




Am I right?
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,993
  • Joined: 05-May 12

Re: xor function

Posted 24 March 2013 - 05:02 PM

You can find out for yourself whether it is correct or not by testing your code with various values.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2