Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: bit-wise operation (+)

by tye (Sage)
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        

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.

    [root@local]#cat #!/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]#./ 33 224 <BR>32[root@local]#

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://956591]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-02-24 01:00 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.