# Sprintf bug in 1.9.0?

**URL:** <https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182>\
**Category:** ruby-talk\
**Created:** [16 February 2004 10:49 UTC](https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182 "2004-02-16T10:49:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)\
**Post date:** [16 February 2004 10:49 UTC](https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182/1 "2004-02-16T10:49:36Z")

</div>

I just compiled the latest release of Ruby 1.9.0, and I’m getting  
unexpected behavior from it…

mark@imac% /usr/local/bin/ruby -e’puts sprintf("%08b",0),RUBY\_VERSION’  
11111110  
1.9.0  
mark@imac% /usr/bin/ruby -e’puts sprintf("%08b",0),RUBY\_VERSION’  
00000000  
1.6.8

it looks like sprintf, when you ask it to pad binary values with zeros,  
is instead padding with ones! It doesn’t seem t have a problem with  
hex, just binary.

-Mark

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [16 February 2004 11:05 UTC](https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182/2 "2004-02-16T11:05:07Z")

</div>

Hi,

At Mon, 16 Feb 2004 19:49:36 +0900,  
Mark Hubbart wrote in [ruby-talk:92961]:

> I just compiled the latest release of Ruby 1.9.0, and I’m getting  
> unexpected behavior from it…
> 
> mark@imac% /usr/local/bin/ruby -e’puts sprintf(“%08b”,0),RUBY\_VERSION’  
> 11111110  
> 1.9.0  
> mark@imac% /usr/bin/ruby -e’puts sprintf(“%08b”,0),RUBY\_VERSION’  
> 00000000  
> 1.6.8

What’s your platform?

$ ruby -v -e’puts sprintf(“%08b”,0),RUBY\_VERSION’  
ruby 1.9.0 (2004-02-15) [i686-linux]  
00000000  
1.9.0

> **···**
>
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [16 February 2004 11:12 UTC](https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182/3 "2004-02-16T11:12:50Z")

</div>

> &nbsp;&nbsp;$ ruby -v -e'puts sprintf("%08b",0),RUBY\_VERSION'

moulon% ruby -ve 'puts sprintf("%08b",0),RUBY\_VERSION'  
ruby 1.8.1 (2003-12-25) [sparc-solaris2.7]  
11111110  
1.8.1  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)\
**Post date:** [16 February 2004 11:41 UTC](https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182/4 "2004-02-16T11:41:42Z")

</div>

> What’s your platform?

Guess I should have mentioned that 🙂 Again, but with full  
version/release/platform tag:

mark@imac% /usr/bin/ruby -ve ‘puts sprintf(“%08b”,0)’  
ruby 1.6.8 (2002-12-24) [powerpc-darwin7.0]  
00000000  
mark@imac% /usr/local/bin/ruby -ve ‘puts sprintf(“%08b”,0)’  
ruby 1.9.0 (2004-02-14) [powerpc-darwin]  
11111110

---

<div class="post-metadata">

**Author:** ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)\
**Post date:** [16 February 2004 11:50 UTC](https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182/5 "2004-02-16T11:50:18Z")

</div>

Hmmmm… I hadn’t thought to check 1.8, but I had it hanging around,  
too, so:  
mark@imac% /usr/local-old/bin/ruby -ve ‘puts sprintf(“%08b”,0)’  
ruby 1.8.0 (2003-08-04) [powerpc-darwin]  
11111110

there we are! I hadn’t noticed that bug before, but then I guess I  
don’t convert to binary strings very often…

> **···**
>
> On Feb 16, 2004, at 3:12 AM, ts wrote:
> 
> > > $ ruby -v -e’puts sprintf(“%08b”,0),RUBY\_VERSION’
> > 
> > moulon% ruby -ve ‘puts sprintf(“%08b”,0),RUBY\_VERSION’  
> > ruby 1.8.1 (2003-12-25) [sparc-solaris2.7]  
> > 11111110  
> > 1.8.1  
> > moulon%
> > 
> > Guy Decoux

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [16 February 2004 12:46 UTC](https://rubytalk.org/t/sprintf-bug-in-1-9-0/10182/6 "2004-02-16T12:46:31Z")

</div>

> moulon% ruby -ve 'puts sprintf("%08b",0),RUBY\_VERSION'  
> ruby 1.8.1 (2003-12-25) [sparc-solaris2.7]

Not really sure (the problem is on line 566 in sprintf.c)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (bignum && !RBIGNUM(val)-\>sign)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = sign\_bits(base, p);

moulon% diff -u sprintf.c~ sprintf.c  
--- sprintf.c~ Thu Jul 3 13:00:18 2003  
+++ sprintf.c Mon Feb 16 13:40:08 2004  
@@ -345,6 +345,7 @@  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long v = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int base, bignum = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int len, pos;  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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val = rb\_big\_clone(val);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb\_big\_2comp(val);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
- val = rb\_big2str(val, base);  
- s = RSTRING(val)-\>ptr;  
+ tmp = rb\_big2str(val, base);  
+ s = RSTRING(tmp)-\>ptr;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (\*s == '-') {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (base == 10) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb\_warning("negative number for %%u specifier");  
@@ -502,8 +503,8 @@  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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