By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,309 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,309 IT Pros & Developers. It's quick & easy.

The Operator &~

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ja***********@gmail.com (Jay Buffington) wrote in message news:<e4**************************@posting.google. com>...
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.
Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.