Monthly Archive

Categories

Exclusive OR

PowerShell has a number of logical operators - -  -and, –or, –not (or !). One I’ve really thought about is the exclusive OR operator –xor.

With a standard –or operator the result is TRUE if one of the statements is TRUE

PS> ('a' -eq 'A') -or ('a' -eq 'z')
True

 

The standard –or operator is also TRUE if both statements are true

PS> ('a' -eq 'A') -or ('Z' -eq 'z')
True

 

The exclusive OR is only TRUE when one of the statements is TRUE and the other is FALSE. So this returns TRUE

PS> ('a' -eq 'A') -xor ('a' -eq 'z')
True

 

But when both statements are TRUE it returns FALSE

PS> ('a' -eq 'A') -xor ('Z' -eq 'z')
False

 

The exclusive OR seems like  bit of an oddity to me.

 

PowerShell has a bitwise xor operator  -- -bxor but outside of that I’m struggling to find a use for –xor. Certainly as an admin I can’t think of a situation where I’d use it.

 

Just out curiosity have you used –xor and if so how?

Comments are closed.