Re: decimal -> Binary

Georg Moritz <[email protected]> Wed, 16 Nov 2011 20:35:02 +0100 (CET)
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
---1454121964-393629654-1321471516=:15400
Content-Type: TEXT/PLAIN; charset=ISO-8859-15; format=flowed
Content-ID: <[email protected]>
Content-Transfer-Encoding: quoted-printable

From the keyboard of Ronald J Kimball [16.11.11,14:14]:

> On Wed, Nov 16, 2011 at 07:49:14PM +0100, Olof Johansson wrote:
>> On 2011-11-16 11:57 -0500, Ronald J Kimball wrote:
>>> No, you are wrong.  s/0*// is sufficient, because /0*/ will always ma=
tch at
>>> the start of the string anyway.

rye

>> You're clearly an expert. I yield. Can you open a bug report with perl
>> and getting this fixed?
>
> It's working as expected for me, so I'm not sure what needs to be fixed=
.

well... the ternary ?: isn't necessary with s/0*// because s/0*// is
always successful, so

print"$_: ",($_=3Dunpack"B*",pack"N",$_)=3D~s/0*//&&$_ for@ARGV;

two strokes ;-)
but as Olof pointed out,

printf"$_:%b\n",$_ for@ARGV

is less convoluted. Why do I forget about printf almost always?
'cause I'm not a C hacker, but a heck parler, I guess ;-)

for some JAPH, see my signature. Latin-1 only, though

cheers,
0--gg-

> % cat tmp.pl
> #!perl -l
> print"$_: ",($_=3Dunpack"B*",pack"N",$_)=3D~s/0*//?$_:$_ for@_=3D@ARGV;
> print"$_: ",($_=3Dunpack"B*",pack"N",$_)=3D~s/^0*//?$_:$_ for@_=3D@ARGV=
;
> % perl tmp.pl 2147483648 3000000000
> 2147483648: 10000000000000000000000000000000
> 3000000000: 10110010110100000101111000000000
> 2147483648: 10000000000000000000000000000000
> 3000000000: 10110010110100000101111000000000
> %
>
> Are you seeing different behavior?
>
> Ronald
>

--=20
_($_=3D" "x(1<<5)."?\n".q=B7/)Oo.  G=B0\        /
                               /\_=AF/(q    /
----------------------------  \__(m.=3D=3D=3D=3D=B7.(_("always off the cr=
owd"))."=B7
");sub _{s./.($e=3D"'Itrs `mnsgdq Gdbj O`qkdq")=3D~y/"-y/#-z/;$e.e && pri=
nt}
---1454121964-393629654-1321471516=:15400--