• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

newbie needs help

Hello experts,

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


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.


1 Solution
& 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.

LuxanaAuthor Commented:
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


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now