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 reply to Re: bit selections by GrandFather
in thread bit selections by biohisham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.