Symbolic representation of logical operators

M

Mark Hobley

Some logical operators have symbolic representation. For example:

The doubleampersand && represents the "and" operator
The doublepipe || represents the "or" operator
The pling ! represents the "not" operator

What is the symbolic representation for the "xor" operator?
I don't appear to be able to find this in any documentation.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
J

Jürgen Exner

Mark said:
Some logical operators have symbolic representation. For example:

The doubleampersand && represents the "and" operator
The doublepipe || represents the "or" operator
The pling ! represents the "not" operator

No, those are actually different operators because e.g. "and" and "&&" have
different precedences.

jue
 
J

Josef Moellers

Mark said:
Some logical operators have symbolic representation. For example:

The doubleampersand && represents the "and" operator
The doublepipe || represents the "or" operator
The pling ! represents the "not" operator

What is the symbolic representation for the "xor" operator?
I don't appear to be able to find this in any documentation.

The caret "^".

BTW as Jürgen has pointed out, "&&" is not a bitwise and but rather a
logical and and it short-circuits, likewise "||" is a logical or:

sub l1 {
print "l1\n"; return 1;
}
sub l2 {
print "l2\n"; return 0;
}
print l2 && l1, "\n";
print l1 || l2, "\n";

You want "&" and "|".

Josef
 
M

Mark Hobley

Josef Moellers said:
The caret "^".

BTW as Jürgen has pointed out, "&&" is not a bitwise and but rather a
logical and and it short-circuits, likewise "||" is a logical or:

I am not looking for a bitwise operator, I am looking for a the
symbolic form of a logical exclusive or to compliment the && and || operators,

The logic being as follows:

false (symbol) false = false
false (symbol) true = true
true (symbol) false = true
true (symbol) true = false (a conventional || or would return true here)

I hope that makes sense.

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
J

Josef Moellers

Mark said:
I am not looking for a bitwise operator, I am looking for a the
symbolic form of a logical exclusive or to compliment the && and || operators,

The logic being as follows:

false (symbol) false = false
false (symbol) true = true
true (symbol) false = true
true (symbol) true = false (a conventional || or would return true here)

I hope that makes sense.

OK, my fault.

How about "!="?

for my $v1 (0,1) {
for my $v2 (0,1) {
print "$v1 != $v2 = ", $v1 != $v2, "\n";
}
}

Josef
 
M

Mark Hobley

Mark Hobley said:
I am not looking for a bitwise operator, I am looking for a the
symbolic form of a logical exclusive or to compliment the && and || operators,

print (2 && 3); # 3, Ok, I was expecting 1, but checked documentation
print (0 && 2); # 0, Ok
print (2 || 3); # 3, Ok, again documented
print (0 || 2); # 2, Ok documented
print (2 ^^ 3); # Syntax error, I hoped 0 (true xor true = false)
print (7 ^ 2); # 5, Ok, but that is a bitwise operator. I am testing logicals
print (7 xor 2); # Empty string, I was expecting 0 (true xor true = false)

I wondered why the xor gives an empty string, and not the second values like
the && and ||, so I thought maybe its because its a named operator, so I tried:

print (0 or 0); # 0, Ok
print (0 and 0); # 0, Ok
print (0 xor 0); # Empty string, why not a 0, like and and or

I know the empty strings and non zero values are fine for logicals, I just
didn't expect some of the results.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
M

Michele Dondi

I know the empty strings and non zero values are fine for logicals, I just
didn't expect some of the results.

Rationale is that Perl tries to be dwimmy, and it generally succeeds.
Of course it can't get it right 100% of the times for 100% of the
people.


Michele
 
M

Mark Hobley

Josef Moellers said:
How about "!="?

It doesn't work for two differing non-zero values:

2 != 3 ; true (an xor would be false here, because both 2 and 3 are true)

It looks like I have to use the named xor and parenthesis.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
X

xhoster

print (2 && 3); # 3, Ok, I was expecting 1, but checked documentation
print (0 && 2); # 0, Ok
print (2 || 3); # 3, Ok, again documented
print (0 || 2); # 2, Ok documented
print (2 ^^ 3); # Syntax error, I hoped 0 (true xor true = false)
print (7 ^ 2); # 5, Ok, but that is a bitwise operator. I am testing
logicals print (7 xor 2); # Empty string, I was expecting 0 (true xor
true = false)

I wondered why the xor gives an empty string, and not the second values
like the && and ||,

||, &&, "and", and "or" all return the value of the last argument that
||needed
to be inspected in order to know what the truth-value of the expression is.
This makes sense, as the boolean value of the last expression it needs to
inspect is always identical to the boolean value of the expression as a
whole, and so returning it gives a correct answer and always much nice code
streamlining. For "xor", this is not the case. It always has to inspect
both arguments, and sometimes it has to yield a value which does not have
the same boolean value as either of its arguments does. Therefore it
creates its result value de novo, rather than just grabbing one of the
arguments.


Xho
 
T

Tad McClellan

Mark Hobley said:
print (2 && 3); # 3, Ok, I was expecting 1, but checked documentation
print (2 || 3); # 3, Ok, again documented
print (7 xor 2); # Empty string, I was expecting 0 (true xor true = false)


You should have instead been expecting any of Perl's false values.

I wondered why the xor gives an empty string, and not the second values like
the && and ||,


Because xor cannot short-circuit, just as its documentation says.

print (0 xor 0); # Empty string, why not a 0, like and and or


&& and || do not return zero, they return the last expression evaluated
(they "short circuit"). No zero here:


perl -we 'print undef or 0'
 
D

Dr.Ruud

Mark Hobley schreef:
I am looking for a the symbolic form of
a logical exclusive or to compliment the && and ||
operators,

The logic being as follows:

false (symbol) false = false
false (symbol) true = true
true (symbol) false = true
true (symbol) true = false

I guess you could use:

!!$v1 ^ !!$v2


For example:

$ perl -wle'
$v1 = "";
$v2 = "0E0";
print !!$v1 ^ !!$v2;
'
1
 
Q

QoS

Some logical operators have symbolic representation. For example:

The doubleampersand && represents the "and" operator
The doublepipe || represents the "or" operator
The pling ! represents the "not" operator

What is the symbolic representation for the "xor" operator?
I don't appear to be able to find this in any documentation.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

#nand gates allow the construction of any of the logical operators.
my $a = 1;
my $b = 1;
my ($o1, $o2, $o3, $o4,);

#o4 is the result of $a xor $b;
$o1 = $a !& $b;
$o2 = $a !& $o1;
$o3 = $b !& $o1;
$o4 = $o2 !& $o3;
 
R

Randal L. Schwartz

Ruud> I guess you could use:

Ruud> !!$v1 ^ !!$v2

Or more simply:

$v1 ? !$v2 : $v2

This will have the proper truth-iness, although there's no easy way to
preserve "last expression evaluated" as with the other short-circuit
operators.
 
A

anno4000

Randal L. Schwartz said:
Ruud> I guess you could use:

Ruud> !!$v1 ^ !!$v2

Or more simply:

$v1 ? !$v2 : $v2

This will have the proper truth-iness, although there's no easy way to
preserve "last expression evaluated" as with the other short-circuit
operators.

Well, you can't expect too much. If the result of the xor is true,
that's fine: there's exactly one true argument, which should be
returned. If the result is false, if we're lucky both arguments
are false, in which case either one can be returned, though there
is no canonical way to choose. If both arguments are true, none
of them can be used, so an "artificial" false has to be returned.

!$v1 ? $v2 :
!$v2 ? $v1 :
!1;

That should be as close as it gets. It arbitrarily returns $v2 in
the false-false case. The final "!1" might as well have been "0",
but this way it returns the Perl-specific Janus-faced false.

Anno
 
B

Bart Lateur

Mark said:
I am not looking for a bitwise operator, I am looking for a the
symbolic form of a logical exclusive or to compliment the && and || operators,

There is none. You might have guessed that by now.

The logical choice would have been the double caret.
 
M

Mark Hobley

Mark Hobley said:
I know the empty strings and non zero values are fine for logicals, I just
didn't expect some of the results.

I found some more apparent weirdness:

print ("apples" || "pears"); # apples
print ("apples" && "pears"); # pears

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
P

Paul Lalli

I found some more apparent weirdness:

print ("apples" || "pears"); # apples
print ("apples" && "pears"); # pears


Why are you calling this "weirdness"? As has already been explained,
the logical operators return the last value they have to evaluate to
determine the truthfullness. So || returns the first true value (or
the last value if they're all false), and && returns the first false
value (or the last value if they're all true).

This allows us to, among other things, use the operators to set
defaults:

my $fruit = $choice || "apples";
#if $chioce is a true value, $fruit = $choice.
#Else, $fruit = "apples";

Paul Lalli
 
M

Mark Hobley

Paul Lalli said:
Why are you calling this "weirdness"? As has already been explained,
the logical operators return the last value they have to evaluate to
determine the truthfullness. So || returns the first true value (or
the last value if they're all false), and && returns the first false
value (or the last value if they're all true).

I'm sorry Paul. I am new to Perl, and my understanding is somewhat limited,
and I feel that the documentation is letting me down here. Anyhow, I am trying
to follow this.

You are saying || returns the first true value.

print (2 || 7); # 7, both true, but the first value is 2, why return 7?
print ("apples" || "pears"); # apples, both true, but why return "apples"?

I am still not yet clear on why the numerical one, returns the opposite
argument. My background is assembly language, and electronics, so I think that
is clouding my judgement or thought process. Maybe I need some good documents
that explain this, with lots of examples. I think the issue is that I am not
able to foresee the behaviour from the either the perlop manual (or certainly
not from the page I was looking at, or from my expensive Professional Perl
Programming book.

Looking at &&, you are saying first false, or last value. That is what I
am getting:

print (2 && 7); # 7, Ok, that matches my understanding of your theory
print ("apples" && "pears"); # pears, Ok, so does that

Excellent, thanks for that! (Unless I understand for a wrong reason, maybe, or
maybe don't understand but, think I do, Huh!)

I am still having a problem with XOR:

print ("apples" xor "pears"); s (true), incorrect, should be 0(false)

With "apples" and "pears" both being true, I was expecting a result of false
here.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/
 
P

Paul Lalli

I'm sorry Paul. I am new to Perl, and my understanding is
somewhat limited, and I feel that the documentation is letting
me down here.

No apologies are necessary, I'm sorry if I implied such. I'm just
confused because I thought this was already explained previously in
this thread. Perhaps not.
Anyhow, I am trying to follow this.

You are saying || returns the first true value.

Yes. Because in order for a chain of arguments connected by || to be
true, only one of those values has to be true. Thus, as soon as we
come to the first true one, Perl can stop looking at the rest. This
is known as short circuit evaluation.
print (2 || 7); # 7, both true, but the first value is 2,

No it doesn't, unless you have some very old or obscure version of
Perl installed:

$ perl -le'print (2 || 7);'
2
$

When you run that, it produces 7? Really? If so, please copy and
paste the output of this:
perl -v
print ("apples" || "pears");
# apples, both true, but why return "apples"?

Because as soon as "apples" was found to be true (all strings other
than "" and "0" are true), there was no need to look at the other
arguments. Perl never even saw "pears" in this evaluation. As soon
as it found one argument to be true, the whole expression is true.
I am still not yet clear on why the numerical one, returns the
opposite argument.

It doesn't. Please check that again.
I think the issue is that I am not able to foresee the behaviour
from the either the perlop manual (or certainly not from the page
I was looking at, or from my expensive Professional Perl
Programming book.

There's really only a couple things you have to make sure you
understand, for this to be clear: The operators (and, or, &&, ||) all
use short circuit evaluation. As soon as it's possible to determine
the results of the entire expression, the operator returns. And they
always return the last value evaluated. For || or or to be true, only
one of the chain of values need be true. For || or or to be false,
they all must be false. The opposite is true for && or and: to be
true, they must all be true. To be false, they must all be false.

If all values are true, || returns the first one, && returns the last:
$x = 1 || 2 || 3 || 4; #1
$x = 1 && 2 && 3 && 4; #4

If all values are false, || returns the last one, && returns the
first:

$x = 0 || "0" || undef || ''; #''
$y = 0 && "0" && undef && ''; #0

If there is a mixture, || returns the first true value, && returns the
first false value:

$x = 0 || 1 || undef || 5; #1
$x = 0 && 1 && undef && 5; #0
Looking at &&, you are saying first false, or last value.
That is what I am getting:

print (2 && 7); # 7, Ok, that matches my understanding
of your theory

It's not a theory, it's simply what is. :)
print ("apples" && "pears"); # pears, Ok, so does that

Excellent, thanks for that! (Unless I understand for a wrong
reason, maybe, or maybe don't understand but, think I do, Huh!)

Nope, you've got it.
I am still having a problem with XOR:

print ("apples" xor "pears"); s (true), incorrect, should be 0
(false)

Again, it doesn't return true for me. I don't know what's making you
think it does. For me, it returns the empty string, which is false:

$ perl -le'print ("apples" xor "pears");'

$
With "apples" and "pears" both being true, I was expecting a
result of false here.

Yes, and that's exactly what it produces. What output are you seeing
that contradicts that? Please copy and paste your actual code and
output, rather than typing in a comment what you think you're seeing.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top