Solved

newbie needs help

Posted on 2004-04-09
2
193 Views
Last Modified: 2010-03-04
Hello experts,

I just start with perl language few days ago.  I need to know what does the & symbol means in the folowing script:

#!/usr/bin/perl
                                                                                                                             

push @array, (1,2,3)[6 & 10];
                                                                                                                             
                                                                                                                             
print @array;

------------------
result: 3

-------------------
what I know that the push function pushes the values of list onto the end of array.  ANd number in square bracets point to list index .   Can somebody give me a hint.

thanks

luxana
0
Comment
Question by:Luxana
2 Comments
 
LVL 8

Accepted Solution

by:
davorg earned 30 total points
ID: 10789957
& is the "bitwise and" operator. It compares the binary representation of two numbers and returns the number that has bits set only where the same bit is set in _both_ of the input numbers.

So, in your example:

6 is 0110 in binary.
10 is 1010 in binary.

They only have one bit in common, so a bitwise and opertion returns 0010. Converting that to decimal gives 2.

So 6 & 10 is 2.

Your code therefore becomes:

push @array, (1,2,3)[2];
                                                                             
print @array;

Which has the same effect as:

push @array, 3;

There is also a "bitwise or" operator which returns a set bit where either of the inputs have a set bit. 6 | 2 is 1110 (which is 14) and a "bitwise exclusive or" which returns a set bit where only on of the inputs have a set bit. 6 ^ 2 is 1100 (which is 12).

See "perldoc perlop" for more details.

Dave...
0
 
LVL 10

Author Comment

by:Luxana
ID: 10790190
Dave ,

I got it:))) You have no idea how many things I tried. Perfect so now I can move on:)))

Thanks for your help Dave

LUXANA

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now