Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XOR? 1

Status
Not open for further replies.

Nebbish

Programmer
Apr 7, 2004
73
US
An easy one: what's the syntax for an exclusive or? I need:

if($true XOR $false)

to pass but not:

if($true XOR $true)

to pass.

Thanks,

Nick
 
This works:
Code:
$a = 1; $b = 0;
if ($a xor $b) {
    print "Hello World\n";
}
Cheers, Neil
 
Heh, I guess I should have tried the obvious first, eh?

Is that the typical syntax, or is there a syntax similar to || and &&?

Thanks,

Nick
 
See here for a full explanation of the operators and precedence:


Code:
printf "%d\n", 4 ^ 3;  ## bitwise XOR (100 ^ 011 = 111)
if(4 xor 3) {
    print "true\n";
} else {
    print "false\n";   ## 4 is true, 3 is true, hence true XOR true is false
}
There is no high precedence logical XOR operator, ie:
Code:
operation    high     low
-------------------------
'and'        &&       and
'or'         ||       or
'xor'        -none-   xor
Cheers, Neil
 
I didn't realize there was a difference between 'and' and '&&'. Thanks for the posts.

-Nick
 
Status
Not open for further replies.

Similar threads

Replies
3
Views
131

Part and Inventory Search

Sponsor

Back
Top