Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Converting negative number to binary with specific width

by RichardK (Parson)
on Nov 03, 2015 at 00:45 UTC ( #1146780=note: print w/replies, xml ) Need Help??


in reply to Converting negative number to binary with specific width

The problem is that the b format is for unsigned integers only, so your -1 gets turned into an unsigned int and printing that overflows your field width, giving you lots of ones.The help for sprintf says :-

%b an unsigned integer, in binary

BTW sprintf always prints the entire number even if it's bigger than the field width, so this is never going to work for 10 bit binary.

Replies are listed 'Best First'.
Re^2: Converting negative number to binary with specific width
by pjkang7 (Novice) on Nov 05, 2015 at 16:52 UTC

    Thanks, I simply did want -1 to show up as 1111111111... of width 10. which, in unsigned int, it would mean infinite (hence lots of ones...) but I just wanted to cut it at ten 1s.

    I did not know sprintf always print the entire number. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1146780]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2018-07-20 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (423 votes). Check out past polls.

    Notices?