in reply to Re: Converting negative number to binary with specific width
in thread Converting negative number to binary with specific width

I was thinking something more along the lines of

c:\@Work\Perl\monks>perl -wMstrict -le "for my $n (-2, -1, 0, 1, 2, 1023, 1024, 1025) { my $bin = reverse unpack 'b10', pack 'v', $n; print qq{'$bin' <= $n}; } " '1111111110' <= -2 '1111111111' <= -1 '0000000000' <= 0 '0000000001' <= 1 '0000000010' <= 2 '1111111111' <= 1023 '0000000000' <= 1024 '0000000001' <= 1025
to get closer to the  'b' formatting of sprintf (although I'm not sure this is any better than substr-inging it). (Update: Also see perlpacktut.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Converting negative number to binary with specific width
by Anonymous Monk on Nov 03, 2015 at 06:31 UTC
    what os the is use of pack 'v' for?

      what os the is use of pack 'v' for?

      The documentation explains it :)

      v An unsigned short (16-bit) in "VAX" (little-endian) order. V An unsigned long (32-bit) in "VAX" (little-endian) order.
        do you have to use it? Isn't unpack 'b10' enough?