pjkang7 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, this is my first question post on PerlMonk. Just fyi, I have searched and tried for some good amount of time (>40min) and decided I could not find out what's wrong with my code. It's a simple one.
Basically, I am trying to convert negative number into specific length (10) binary. And it's simply refusing to do that. Here's the code:
my $neg = -1; my $negBin = sprintf ("010.10b", $neg); print $negBin; #returns a lot of 1s instead of 11_1111_1111. (without +underscores)
Another problem i had was if I am trying to convert 1024 into binary number:
my $num = 1024; my $bin = sprintf ("010.10b", $num); print $bin; #returns 100_0000_0000 instead of 00_0000_0000
Apparently, there's something wrong with my sprintf formating....and I can't figure it out. I have used substr to simply cut the string to my desired length as a way around it but I am still interested to find out what I did wrong... I turn to the wisdom of PerlMonks out there. Thank you.
|
|---|