Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^7: 'xor' operator is not a sibling to 'or' and 'and'?

by pryrt (Prior)
on Dec 18, 2019 at 22:57 UTC ( #11110361=note: print w/replies, xml ) Need Help??


in reply to Re^6: 'xor' operator is not a sibling to 'or' and 'and'?
in thread 'xor' operator is not a sibling to 'or' and 'and'?

Stop thinking in terms of bitwise; this is Boolean. The "evaluated expressions" are logical values. 2 is logic-true, 3 is logic-true, 2 and 3 returns 3, which is logic-true. It uses the last evaluated expression as the logical value.

C:\usr\local\share\PassThru\perl>perl -le "print +('george' and 'graci +e')" gracie C:\usr\local\share\PassThru\perl>perl -le "print +('0' and 'gracie')" 0 C:\usr\local\share\PassThru\perl>perl -le "print +('' and 'gracie')" C:\usr\local\share\PassThru\perl>perl -le "print +('george' and 'false +')" false C:\usr\local\share\PassThru\perl>perl -le "print +('george' and '')" C:\usr\local\share\PassThru\perl>perl -le "print +(2 and 4)" 4 C:\usr\local\share\PassThru\perl>perl -le "print +(2 && 4)" 4 C:\usr\local\share\PassThru\perl>perl -le "print +(2 & 4)" 0

Replies are listed 'Best First'.
Re^8: 'xor' operator is not a sibling to 'or' and 'and'?
by rsFalse (Hermit) on Dec 18, 2019 at 23:13 UTC
    >> Stop thinking in terms of bitwise; this is Boolean.

    If this is Boolean, shouldn't it return 1 or ''/0?
    p.s. I haven't write anything about bitwise.
      If this is Boolean, shouldn't it return 1 or ''/0?

      Technically, TRUE and FALSE. 1 and 0 are just one way of representing those concepts. The TI99-4/A TI-BASIC used -1 to represent TRUE. Perl uses just about anything for TRUE, and a more limited set (including 0, '', and undef) for FALSE; as a result, Perl just picked the most convenient TRUE or FALSE for implementing with the short-circuiting of and / or, which turned out to be the last expression evaluated -- because with short-circuiting, the last expression evaluated always has the same logical value as the entire and / or. With xor, neither the last-evaluated nor the first-evaluated expression contains the correct logical value, so Perl has to do extra work to come to the right logical conclusion, so then Perl uses its default TRUE and FALSE representations, rather than just picking the value from one of the evaluated expressions.

      p.s. I haven't write anything about bitwise.

      Sorry, you're right. I was going on very little sleep yesterday, and had misinterpreted your original post in a way that made me conclude you were expecting results similar to a bitwise operation; re-reading after a better night's sleep, I don't see that anymore.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11110361]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2020-06-05 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (40 votes). Check out past polls.

    Notices?