Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: bit-wise operation (+)

by tye (Cardinal)
on Feb 28, 2012 at 06:42 UTC ( #956591=note: print w/ replies, xml ) Need Help??


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        


Comment on Re: bit-wise operation (+)
Select or Download Code
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]#

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://956591]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2015-07-07 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls