473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

The Operator &~

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
2 2734
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
3
by: Nathan Sokalski | last post by:
I have a VB.NET function that I am using in an ASP.NET page. The code creates a String, which contains && (the JavaScript Logical AND operator) and is used as part of the JavaScript sent to the...
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
7
by: randysimes | last post by:
I have to overload this operator to cin a character array to eventually put the strings in lexicographical order. The is unknown and therefore will change accordingly. I do not know how to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.