toro_the_third has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm new here and let me apologize for any inconvenience regarding this post.
so, my question is that I'm trying to make a script that reads 2 arguments and returns result of bit-wise operation AND.

#!/usr/bin/perl use strict; use warning; my $arg00=shift ( @ARGV ); my $arg01=shift ( @ARGV ); #printf $arg00+$arg01."\n"; printf sprintf( $arg00&$arg01 )."\n";

when I run test.pl 33 224, it returns result as 22.
if I run printf sprintf( 33&224 )."\n"; it returns 32. also if I comment out the # before the printf, it also returns 32.
could any one give me an advice about this difference?
regards,
toro

Replies are listed 'Best First'.
Re: bit-wise operation (+)
by tye (Sage) on Feb 28, 2012 at 06:42 UTC

    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        

      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]#
Re: bit-wise operation
by NetWallah (Canon) on Feb 28, 2012 at 06:13 UTC
    Both printf and sprintf take TWO arguments:

    printf FORMAT, LIST
    You supplied only one, so it prints the content of what you supplied.

    Try using "%X" as a format, to print hex, or review the sprintf documentation for more options.

    I do not have an explanation for your "32" result, but I can reproduce it. Perhaps a wiser monk ....

                “PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.”
            ― Jon Ribbens

      Hi, thank you for the prompt reply. I have fixed the printf part. but it still wouldn't work correct.

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

      It should return 32 but returns 22.
      I cant figure out why this happens...
      regards,
      toro