The Operator &~

J

Jay Buffington

I was looking through some very badly written code at work today and I
came across this line:
my $hold_status=$invoice->{'HOLD_STATUS'} &~ HS_MAIL_IN_PHOTO;

HS_MAIL_IN_PHOTO is defined as a constant equal to 2.

I think the original author meant == instead of &~.

I tried this:
#!/usr/bin/perl -w

use strict;

my $num1 = 0x110;
my $num2 = 0x111;
my $num3 = 0x011;

print "num1 &~ num2\n" if $num1 &~ $num2;
print "num2 &~ num3\n" if $num2 &~ $num3;
print "num1 &~ num3\n" if $num1 &~ $num3;

and got this output:
num1 &~ num2

Which has convinced me that this is a bitwise pattern matching
operator.

Are there any legitimate uses for &~? I can't think of one. I can't
find &~ documented anywhere. I've looked in perlop and the Camel
Book.

Why does this exist?

As a side note, I also played with &&~, which works like this:
print "foo" if (&sub1 &&~ &sub2); # execute sub1 and sub2 and only
print foo if sub1 returns true.

Jay
 
N

nobull

I was looking through some very badly written code at work today and I
came across this line:
my $hold_status=$invoice->{'HOLD_STATUS'} &~ HS_MAIL_IN_PHOTO;

HS_MAIL_IN_PHOTO is defined as a constant equal to 2.

I think the original author meant == instead of &~.

No, it is not uncommon to use bitwise operations for flags. So this
mean that
$invoice->{HOLD_STATUS} is a number being used as a bit array and we
wist to copy it into $hold_status but without the flag
HS_MAIL_IN_PHOTO.
I tried this:
#!/usr/bin/perl -w

use strict;

my $num1 = 0x110;
my $num2 = 0x111;
my $num3 = 0x011;

print "num1 &~ num2\n" if $num1 &~ $num2;
print "num2 &~ num3\n" if $num2 &~ $num3;
print "num1 &~ num3\n" if $num1 &~ $num3;

and got this output:
num1 &~ num2

Which has convinced me that this is a bitwise pattern matching
operator.

Yes, it returns a number whose binary representation has 1s where
there were 1s in the LHS but not in RHS.

Used in a boolean context it tells you if there are any bits set in
the LHS that weren't in the RHS.

Of course all this assumes the LHS is a number. The bitwise operators
in Perl are about the only thing that will treat $num1='666' and
$num1=666 differently (for details RTFM).
Are there any legitimate uses for &~? I can't think of one.

Yes there are many. I must admit to not having used in in Perl but
I've used it often in C (and indeed in SQL).
I can't find &~ documented anywhere. I've looked in perlop and the Camel
Book.

Why does this exist?

Because geiven that there's no token &~ in Perl then perl will
interpret it as two tokens & and ~ (which are both documented in the
aforementioned places).
As a side note, I also played with &&~, which works like this:
print "foo" if (&sub1 &&~ &sub2); # execute sub1 and sub2 and only
print foo if sub1 returns true.

Well actually if sub2 returns ~0 then it won't print foo.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 
J

Joe Smith

Jay said:
I was looking through some very badly written code at work today and I
came across this line:
my $hold_status=$invoice->{'HOLD_STATUS'} &~ HS_MAIL_IN_PHOTO;

HS_MAIL_IN_PHOTO is defined as a constant equal to 2.

I think the original author meant == instead of &~.

How to set a bit:
$variable = $variable | $bit;

How to clear a bit:
$variable = $variable & ~$bit;

$before = 1 | 2 | 4 | 8;
$after = $before & ~2;
printf "before=%04b after=%04b\n",$before,$after;

-Joe
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top