# xor function

• (2 Pages)
• 1
• 2

## 18 Replies - 1541 Views - Last Post: 24 March 2013 - 05:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316177&amp;s=c11de7a38e530ad12cf9b95f1cb42f09&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 CTphpnwb

• D.I.C Lover

Reputation: 3320
• Posts: 11,754
• Joined: 08-August 08

## Re: xor function

Posted 22 March 2013 - 11:36 AM

chenfire, on 22 March 2013 - 12:18 PM, said:

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

Wrong. Your call from main() would need to be in a loop because your function is not recursive.

### #17 chenfire

Reputation: 2
• 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

### #18 chenfire

Reputation: 2
• 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?

### #19 Skydiver

• Code herder

Reputation: 4248
• Posts: 13,583
• 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.