# The Operator &~

Discussion in 'Perl' started by Jay Buffington, Sep 9, 2004.

1. ### Jay BuffingtonGuest

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

Jay Buffington, Sep 9, 2004

2. ### Guest

(Jay Buffington) wrote in message news:<>...
> 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.

, Sep 10, 2004

3. ### Joe SmithGuest

Jay Buffington wrote:

> 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

Joe Smith, Sep 15, 2004