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

This code fills up an array from command line then proceeds to apply unary negation on the decimal and binary values for each element of the array. in the negative binary however, I am trying to only print the first 8 bits for any value in the list, but I could not achieve so using the & operator. Any Suggestions?
also (int) in the 16th line has been unavoidable for me to make this c +ode work since I wanted to work with numbers
#!/usr/local/bin/perl use strict; use warnings; my @list; print "Enter a list and q when finished\n"; while(<>){ if(/^q/){ &comp; }else{ chomp; int; push (@list,$_); } } sub comp{ print "+v\t\t-v\t\t+v\t\t-v\n"; foreach my $element (@list){ my $negative= ~$element; printf "%d\t\t%d\t\t%#b\t\t%#32b\t\n", $element, $nega +tive, $element,$negative; } }
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Replies are listed 'Best First'.
Re: bit selections
by GrandFather (Saint) on Jun 29, 2009 at 22:52 UTC

    In your sample code int is not doing anything. Indeed, if you actually run it with the strictures you show you will get the warning 'Useless use of int in void context at ...'.

    Don't use &subName to call a sub. Use subName () instead. The &subName version of the call has some potentially nasty habits.

    the following cleaned up sample may help:

    use strict; use warnings; my @list; while (<DATA>) { chomp; push (@list, $_); } comp (); sub comp { printf "%-12s%-12s%-12s%-12s\n", (' +v', ' -v') x 2; foreach my $element (map int, @list) { my $negative = ~$element; printf "%11d %11d %#11.8b %#11.8b\n", $element, $negative, 0xFF & $element, 0xFF & $negative; } } __DATA__ 1 2 10 -1 -2 -10 123456 -123456 255 256

    Prints:

    +v -v +v -v 1 -2 0b00000001 0b11111110 2 -3 0b00000010 0b11111101 10 -11 0b00001010 0b11110101 -1 0 0b11111111 0b00000000 -2 1 0b11111110 0b00000001 -10 9 0b11110110 0b00001001 123456 -123457 0b01000000 0b10111111 -123456 123455 0b11000000 0b00111111 255 -256 0b11111111 0b00000000 256 -257 0b00000000 0b11111111

    Note the use of map in the for loop to apply int to each of the array elements.

    Update fixed stupid paste error (thanks toolic)!


    True laziness is hard work

      In your sample code int is not doing anything.

      Yes and no. It has an effect, but it's not visible since he's not using |, & and/or ^ at the moment.

      >perl -le"$_='7'; int if $ARGV[0]; print $_ | '123'" 0 723 >perl -le"$_='7'; int if $ARGV[0]; print $_ | '123'" 1 127

      The effect:

      >perl -MDevel::Peek -e"$_='0'; Dump $_; int; Dump $_;" SV = PV(0x236054) at 0x18447c4 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1824064 "0"\0 CUR = 1 LEN = 4 SV = PVIV(0x238030) at 0x18447c4 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 0 PV = 0x1824064 "0"\0 CUR = 1 LEN = 4

      The better solution:

      push @list, 0+$_;
Re: bit selections
by Anonymous Monk on Jun 29, 2009 at 22:12 UTC
    warn ord 'c'; warn chr 99; warn unpack 'c', 'cowboy'; __END__ 99 at - line 1. c at - line 2. 99 at - line 3.
      Could you elaborate a bit more since I understand nothing of this code your replied !!!!
      Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind
        Impossible, you must understand something :)
      What were you trying to do using the ord function would get the unicode value and then you are translating it and then again getting it when this node here I was asking for something else!!!!
      Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind