# Understanding Xor

Page 1 of 1

## 5 Replies - 1029 Views - Last Post: 25 September 2012 - 03:13 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=293207&amp;s=a1448d480def3eabdd17ead8fd4f35b8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hiatus

Reputation: 0
• Posts: 81
• Joined: 27-May 09

# Understanding Xor

Posted 25 September 2012 - 02:25 PM

Hey all, im having trouble understanding what this means:

commutative(op,S) is a function that takes two arguments and returns True only if the first argument op, an operator that takes two arguments, is commutative on the finite set S.

Would op be an actual operator such as +,-,/,*?

And if one were to test this function, how would one go about doing so?

Thanks.

This post has been edited by Hiatus: 25 September 2012 - 02:26 PM

Is This A Good Question/Topic? 0

## Replies To: Understanding Xor

### #2 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Understanding Xor

Posted 25 September 2012 - 02:50 PM

Operations are not limited to the basic ones (+, -, /, *) for example XOR could be classed as an operation, AND, OR, EQUIVALANCE.. don't you have some reference as to what operations are valid? Or perhaps any function at all?

In Python, functions are first class so, they can be passed as parameters so that isn't a problem. Commutative basically means that the operation will work if you switch the operands around:

So,

4 + 5 is equal to 5 + 4 so "+" is commutative (or add(x, y) as a function)

4 - 5 is NOT equal to 5 - 4 so "-" is not commutative (or subtract(x, y) as a function)

How would you go about testing if an operand (or function) is commutative or not?

This post has been edited by Simown: 25 September 2012 - 02:56 PM

### #3 Hiatus

Reputation: 0
• Posts: 81
• Joined: 27-May 09

## Re: Understanding Xor

Posted 25 September 2012 - 02:59 PM

Simown, on 25 September 2012 - 03:50 PM, said:

Operations are not limited to the basic ones (+, -, /, *) for example XOR could be classed as an operation, AND, OR, EQUIVALANCE.. don't you have some reference as to what operations are valid? Or perhaps any function at all?

In Python, functions are first class so, they can be passed as parameters so that isn't a problem. Commutative basically means that the operation will work if you switch the operands around:

So,

4 + 5 is equal to 5 + 4 so "+" is commutative (or add(x, y) as a function)

4 - 5 is NOT equal to 5 - 4 so "-" is not commutative (or subtract(x, y) as a function)

How would you go about testing if an operand (or function) is commutative or not?

Hi, thanks for the informative response!

Well to test if something was commutative in a set S, then I would go about testing if S[0] + S[1] = S[1] + S[0] and traverse the set from there. Is this correct logic? And if so, would I need a for loop to keep track of the index?

### #4 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Understanding Xor

Posted 25 September 2012 - 03:07 PM

Yes, that's exactly how to test! But, we want to test every element against every other element to ensure the entire set of items is commutative against the same set without writing S[0] + S[1] = S[1] + S[0].... S[39] + S[40]..... + S[81]... well, you get it.

So, we need to loop, I assume you understand them?

It will go something like this:
```def commutative(op, S):
# First iteration of the set
for first in S:
# Second iteration of the same set so every item is compared against the first
for second in S:
# Remember the double equals
if(op(first,second) == op(second, first))

```

Ok, stop there, now what's left? Finish the code!

This post has been edited by Simown: 25 September 2012 - 11:46 PM

### #5 Hiatus

Reputation: 0
• Posts: 81
• Joined: 27-May 09

## Re: Understanding Xor

Posted 25 September 2012 - 03:10 PM

Simown, on 25 September 2012 - 04:07 PM, said:

Yes, that's exactly how to test! But, we want to test every element against every other element to ensure the entire set it commutative in the set without writing S[0] + S[1] = S[1] + S[0].... S[39] + S[40]..... + S[81]... well, you get it.

So, we need to loop, I assume you understand them?

It will go something like this:
```def(op, S):
# First iteration of the set
for first in S:
# Second iteration of the same set
for second in S:
# Remember the double equals
if(op(first,second) == op(second, first)

```

Ok, stop there, now what's left? Finish the code!

Thank you so much!

Ill try to finish the code from there.

Thanks again for all of your help!

Rep +1

### #6 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Understanding Xor

Posted 25 September 2012 - 03:13 PM

Sure, post back here if you have any more problems and I'm sure anyone will be happy to help.

But really, it's nearly done

This post has been edited by Simown: 25 September 2012 - 03:14 PM