Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Boolean operation: (A & B) vs (A * B)

by Anonymous Monk
on Jan 28, 2016 at 13:29 UTC ( [id://1153858]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi

In Boolean function evaluations using Perl, what is the difference between these two expressions:

#1 Y = (A & B) #2 Y = (A * B)

I cannot seem to find documentation on the #2 form of expression, even though I have seen #2 in Boolean expressions used in Perl. For example, this thread has #2 style: parser for evaluating arbitrarily complex boolean function in a string

Bill Murphy

Replies are listed 'Best First'.
Re: Boolean operation: (A & B) vs (A * B)
by LanX (Saint) on Jan 28, 2016 at 13:43 UTC
    In mathematics (i.e. boolean algebra) it is common to denote * for and and + for or IFF the alphabet is restricted to boolean values 0 and 1.

    NOTE: 1 + 1 is mapped to 1 there.

    This works in Perl somehow. (1+1 =2 is still true)

    But don't ! It's not recommended ... rather a workaround in languages without boolean operators.²

    Just sketch the truth table to see how it works.

    Better use && and || instead.

    > Y = (A & B)

    that's definitely wrong, because & is for a bitwise and not a logical.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    UPDATE

    to elaborate more: the standard false value in Perl is not 0 but empty string '' . While the latter will be evaluated to 0 in arithmetic operations it might cause a confusion.

    DB<7> print !!0 DB<8> print !!1 1

    footnotes

    ²) and only for simple cases, b/c you risk a number overflow when multiplying true values.

    > perl -E "say ((1+1) * (1+1) * (1+1));" 8
Re: Boolean operation: (A & B) vs (A * B)
by GotToBTru (Prior) on Jan 28, 2016 at 13:40 UTC

    The multiplication operator and the bitwise AND are obviously not the same, but if the only values for A and B are 0 or 1, A * B will produce the same truth table as A && B.

    Update: replaced & with && per LanX.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Boolean operation: (A & B) vs (A * B)
by hippo (Bishop) on Jan 28, 2016 at 13:40 UTC

    A * B is a numerical multiplication. If the (perl) values of A and B can only be 1 and 0 then this is equivalent to a Boolean AND.

    A & B is a bitwise AND. Again, if 1 and 0 are the only values this is also equivalent to a Boolean AND.

    Whether or not these expressions are entirely equivalent depends on the restrictions of the values to the binary set {0, 1}.

      > If the (perl) values of A and B can only be 1 and 0 then this is equivalent to a Boolean AND.

      well it's a bit more complicated... 1+1 = 2, see my post Re: Boolean operation: (A & B) vs (A * B)

      Update

      as pointed out neither the OP nor hippo used +, I saw the wider scope ...

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Boolean operation: (A & B) vs (A * B)
by kennethk (Abbot) on Jan 28, 2016 at 15:58 UTC
Re: Boolean operation: (A & B) vs (A * B)
by u65 (Chaplain) on Jan 28, 2016 at 13:43 UTC

    To ease analysis that node link is 399322.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1153858]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-23 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found