in reply to bit-wise operation

Ugh. The documentation on & in perlop is split into two, one half under "Bitwise And" and the other half under "Bitwise String Operators" and neither half even hints at how & can do both of these different operations.

So, I guess it is somewhere else in the documentation (or perhaps even only in earlier versions of the documentation) where I have read that & does bit-wise integer 'and' if either of the two arguments have been recently used as a number. However, if both arguments have only ever been used as strings, then bit-wise string 'and' is done.

@ARGV contains the string arguments given on the command line. This (the bit-wise operators, & and |) is one of the quite rare cases where this kind of tricky detail matters. I'd probably change your code to use:

my $arg00 = 0 + shift @ARGV;

to force $arg00 to be treated as a number.

Or you can force it right next to the operator:

(0+$arg00) & $arg01

- tye        

Replies are listed 'Best First'.
Re^2: bit-wise operation (+)
by toro_the_third (Novice) on Feb 28, 2012 at 07:03 UTC

    Thank you very much for your great help.
    I have fixed the variable declaration part and worked fine.
    Now, I understand why it worked when I put
    printf $arg00+$arg01."\n";
    between this code. Thanks again for your help, I have been suffering for days about this.
    thanks,
    toro

    [root@local]#cat test.pl #!/usr/bin/perl use strict; use warning; my $arg00=0+shift ( @ARGV ); my $arg01=0+shift ( @ARGV ); #printf $arg00+$arg01."\n"; printf( "%d", $arg00 & $arg01 ); [root@local]#./test.pl 33 224 <BR>32[root@local]#