# Ruby's Bignum

**URL:** <https://rubytalk.org/t/rubys-bignum/27961>\
**Category:** ruby-talk\
**Created:** [4 June 2006 06:34 UTC](https://rubytalk.org/t/rubys-bignum/27961 "2006-06-04T06:34:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Patterson2](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Aaron\_Patterson2](https://rubytalk.org/u/Aaron_Patterson2)\
**Post date:** [4 June 2006 06:34 UTC](https://rubytalk.org/t/rubys-bignum/27961/1 "2006-06-04T06:34:59Z")

</div>

Hi everyone. I'm porting some C code to pure ruby, and I would like to  
keep Ruby from turning my numbers in to Bignums. Is that possible?

For example, in C:

&nbsp;&nbsp;printf("number: %d\n", 0xd76aa478);

Prints "number: -680876936"

In ruby:

irb(main):001:0\> puts 0xd76aa478  
3614090360  
=\> nil

Is it possible to get ruby to behave the same way as C? This seems like  
something easy to do, I'm just having a hard time figuring it out.  
Thank you!

--Aaron

---

<div class="post-metadata">

**Author:** ![Park\_Heesob1](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Park\_Heesob1](https://rubytalk.org/u/Park_Heesob1)\
**Post date:** [4 June 2006 07:23 UTC](https://rubytalk.org/t/rubys-bignum/27961/2 "2006-06-04T07:23:38Z")

</div>

Hi,

> From: Aaron Patterson \<aaron\_patterson@speakeasy.net\>  
> Reply-To: ruby-talk@ruby-lang.org  
> To: ruby-talk@ruby-lang.org (ruby-talk ML)  
> Subject: Ruby's Bignum  
> Date: Sun, 4 Jun 2006 15:34:59 +0900
> 
> Hi everyone. I'm porting some C code to pure ruby, and I would like to  
> keep Ruby from turning my numbers in to Bignums. Is that possible?
> 
> For example, in C:
> 
> &nbsp;&nbsp;printf("number: %d\n", 0xd76aa478);
> 
> Prints "number: -680876936"
> 
> In ruby:
> 
> irb(main):001:0\> puts 0xd76aa478  
> 3614090360  
> =\> nil
> 
> Is it possible to get ruby to behave the same way as C? This seems like  
> something easy to do, I'm just having a hard time figuring it out.  
> Thank you!

Try this:

irb(main):001:0\> puts [0xd76aa478].pack('L').unpack('l').first  
-680876936  
=\> nil

Regards,

Park Heesob

---

<div class="post-metadata">

**Author:** ![David\_Balmain1](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@David\_Balmain1](https://rubytalk.org/u/David_Balmain1)\
**Post date:** [4 June 2006 07:26 UTC](https://rubytalk.org/t/rubys-bignum/27961/3 "2006-06-04T07:26:11Z")

</div>

> Hi everyone. I'm porting some C code to pure ruby, and I would like to  
> keep Ruby from turning my numbers in to Bignums. Is that possible?
> 
> For example, in C:
> 
> &nbsp;&nbsp;printf("number: %d\n", 0xd76aa478);
> 
> Prints "number: -680876936"
> 
> In ruby:
> 
> irb(main):001:0\> puts 0xd76aa478  
> 3614090360  
> =\> nil
> 
> Is it possible to get ruby to behave the same way as C? This seems like  
> something easy to do, I'm just having a hard time figuring it out.  
> Thank you!

Hi Aaron,

Short answer, no. In Ruby, as you know, everything is an object and in  
the C code implementing ruby, this is represented by pointers to these  
objects. These pointers have a type of VALUE which is basically a  
32-bit integer. But it would be pretty inefficient to store integers  
as memory-allocated objects so instead it stores them in the same  
32-bit integer. (This is true for FixInt, BigNum is an object ref). So  
how does the interpreter know the difference between an object  
reference and a FixInt? It relies on the fact that all memory pointers  
are are on 4 or 8-byte boundaries so that leaves at least the last 2  
bits free. These last two bits are used to specify whether VALUE is an  
integer, a refence or possibly a symbol, true or false value or nil.  
This means that there are only 30 bits left to specify the value of  
FixInt. So the maximum value you can store in a FixInt is 0x3FFFFFFF  
and the minimum is -0x40000000.

irb(main):007:0\> 0x40000000.class  
=\> Bignum  
irb(main):008:0\> 0x3fffffff.class  
=\> Fixnum  
irb(main):009:0\> -0x40000000.class  
=\> Fixnum  
irb(main):010:0\> -0x40000001.class  
=\> Bignum

Hope that makes sense.

Cheers,  
Dave

> **···**
>
> On 6/4/06, Aaron Patterson \<aaron\_patterson@speakeasy.net\> wrote:
> 
> > --Aaron

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [4 June 2006 18:11 UTC](https://rubytalk.org/t/rubys-bignum/27961/4 "2006-06-04T18:11:55Z")

</div>

Aaron Patterson wrote:

> Hi everyone. I'm porting some C code to pure ruby, and I would like to  
> keep Ruby from turning my numbers in to Bignums. Is that possible?
> 
> For example, in C:
> 
> &nbsp;&nbsp;printf("number: %d\n", 0xd76aa478);
> 
> Prints "number: -680876936"

It's just another variation on pack/unpack, but with bit-struct (from RAA):

require 'bit-struct'

class C \< BitStruct  
&nbsp;&nbsp;signed :foo, 32  
end

c = C.new  
c.foo = 0xd76aa478

printf("number: %d\n", c.foo)  
# ==\> number: -680876936

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![David\_Balmain1](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@David\_Balmain1](https://rubytalk.org/u/David_Balmain1)\
**Post date:** [4 June 2006 07:38 UTC](https://rubytalk.org/t/rubys-bignum/27961/5 "2006-06-04T07:38:27Z")

</div>

Sorry, this answer should have been under "and I would like to keep  
Ruby from turning my numbers in to Bignums. Is that possible?"

To cast an Integer to a 32-bit signed integer you can use pack/unpack  
like Park said, or you could do it like this;

def to\_32\_bit\_signed(val)  
&nbsp;&nbsp;&nbsp;&nbsp;val |= -0x100000000 if (val & 0x80000000) \> 0  
&nbsp;&nbsp;&nbsp;&nbsp;val  
end

> **···**
>
> On 6/4/06, David Balmain \<dbalmain.ml@gmail.com\> wrote:
> 
> > On 6/4/06, Aaron Patterson \<aaron\_patterson@speakeasy.net\> wrote:  
> > \> Hi everyone. I'm porting some C code to pure ruby, and I would like to  
> > \> keep Ruby from turning my numbers in to Bignums. Is that possible?  
> > \>  
> > \> For example, in C:  
> > \>  
> > \> printf("number: %d\n", 0xd76aa478);  
> > \>  
> > \> Prints "number: -680876936"  
> > \>  
> > \> In ruby:  
> > \>  
> > \> irb(main):001:0\> puts 0xd76aa478  
> > \> 3614090360  
> > \> =\> nil  
> > \>  
> > \> Is it possible to get ruby to behave the same way as C? This seems like  
> > \> something easy to do, I'm just having a hard time figuring it out.  
> > \> Thank you!
> > 
> > Hi Aaron,
> > 
> > Short answer, no. In Ruby, as you know, everything is an object and in  
> > the C code implementing ruby, this is represented by pointers to these  
> > objects. These pointers have a type of VALUE which is basically a  
> > 32-bit integer. But it would be pretty inefficient to store integers  
> > as memory-allocated objects so instead it stores them in the same  
> > 32-bit integer. (This is true for FixInt, BigNum is an object ref). So  
> > how does the interpreter know the difference between an object  
> > reference and a FixInt? It relies on the fact that all memory pointers  
> > are are on 4 or 8-byte boundaries so that leaves at least the last 2  
> > bits free. These last two bits are used to specify whether VALUE is an  
> > integer, a refence or possibly a symbol, true or false value or nil.  
> > This means that there are only 30 bits left to specify the value of  
> > FixInt. So the maximum value you can store in a FixInt is 0x3FFFFFFF  
> > and the minimum is -0x40000000.
> > 
> > irb(main):007:0\> 0x40000000.class  
> > =\> Bignum  
> > irb(main):008:0\> 0x3fffffff.class  
> > =\> Fixnum  
> > irb(main):009:0\> -0x40000000.class  
> > =\> Fixnum  
> > irb(main):010:0\> -0x40000001.class  
> > =\> Bignum
> > 
> > Hope that makes sense.
> > 
> > Cheers,  
> > Dave

---

<div class="post-metadata">

**Author:** ![Aaron\_Patterson2](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Aaron\_Patterson2](https://rubytalk.org/u/Aaron_Patterson2)\
**Post date:** [4 June 2006 17:58 UTC](https://rubytalk.org/t/rubys-bignum/27961/6 "2006-06-04T17:58:05Z")

</div>

Thanks Park! Thats exactly what I was looking for.

--Aaron

> **···**
>
> On Sun, Jun 04, 2006 at 04:23:38PM +0900, Park Heesob wrote:
> 
> > Try this:
> > 
> > irb(main):001:0\> puts [0xd76aa478].pack('L').unpack('l').first  
> > -680876936  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 June 2006 09:32 UTC](https://rubytalk.org/t/rubys-bignum/27961/7 "2006-06-04T09:32:54Z")

</div>

And if you need this in multiple places of the program then it's  
probably worth while to create a new subclass of Integer that  
restricts itself to 32 bit calculations

Int32 \< Integer  
&nbsp;&nbsp;def initialize(x)  
&nbsp;&nbsp;&nbsp;&nbsp;@val = to\_32(x)  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def to\_int ...  
&nbsp;&nbsp;def coerce(a,b) ...  
&nbsp;&nbsp;def hash() ...  
&nbsp;&nbsp;def eql?(x)  
&nbsp;&nbsp;def ==(x) ...  
&nbsp;&nbsp;def +(x) ...  
&nbsp;&nbsp;def -(x) ...  
private  
&nbsp;&nbsp;def to\_32(x) ...

end

It's a nice excercise to get used to operators and numercial coercion. 🙂

Maybe we can even generalize that and put it into the standard lib -  
something like a restricted integer that calculates with a max number  
of bits.

Kind regards

robert

> **···**
>
> 2006/6/4, David Balmain \<dbalmain.ml@gmail.com\>:
> 
> > On 6/4/06, David Balmain \<dbalmain.ml@gmail.com\> wrote:  
> > To cast an Integer to a 32-bit signed integer you can use pack/unpack  
> > like Park said, or you could do it like this;
> > 
> > def to\_32\_bit\_signed(val)  
> > &nbsp;&nbsp;&nbsp;&nbsp;val |= -0x100000000 if (val & 0x80000000) \> 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;val  
> > end
> 
> --  
> Have a look: [http://www.flickr.com/photos/fussel-foto/](http://www.flickr.com/photos/fussel-foto/)

---

<div class="post-metadata">

**Author:** ![Lloyd\_Zusman](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@Lloyd\_Zusman](https://rubytalk.org/u/Lloyd_Zusman)\
**Post date:** [4 June 2006 14:17 UTC](https://rubytalk.org/t/rubys-bignum/27961/8 "2006-06-04T14:17:20Z")

</div>

"Robert Klemme" \<shortcutter@googlemail.com\> writes:

> **···**
>
> > 2006/6/4, David Balmain \<dbalmain.ml@gmail.com\>:
> > 
> > > On 6/4/06, David Balmain \<dbalmain.ml@gmail.com\> wrote:  
> > > To cast an Integer to a 32-bit signed integer you can use pack/unpack  
> > > like Park said, or you could do it like this;
> > > 
> > > def to\_32\_bit\_signed(val)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;val |= -0x100000000 if (val & 0x80000000) \> 0  
> > > &nbsp;&nbsp;&nbsp;&nbsp;val  
> > > end
> > 
> > And if you need this in multiple places of the program then it's  
> > probably worth while to create a new subclass of Integer that  
> > restricts itself to 32 bit calculations
> > 
> > Int32 \< Integer  
> > &nbsp;&nbsp;def initialize(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@val = to\_32(x)  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;def to\_int ...  
> > &nbsp;&nbsp;def coerce(a,b) ...  
> > &nbsp;&nbsp;def hash() ...  
> > &nbsp;&nbsp;def eql?(x)  
> > &nbsp;&nbsp;def ==(x) ...  
> > &nbsp;&nbsp;def +(x) ...  
> > &nbsp;&nbsp;def -(x) ...  
> > private  
> > &nbsp;&nbsp;def to\_32(x) ...
> > 
> > end
> > 
> > It's a nice excercise to get used to operators and numercial coercion. 🙂
> > 
> > Maybe we can even generalize that and put it into the standard lib -  
> > something like a restricted integer that calculates with a max number  
> > of bits.
> 
> This is indeed a good Ruby exercise, but if it ever ends up in the  
> standard library, I would then vote for it to be re-implemented in C, as  
> the Ruby version would be hugely less efficient than the native  
> 32-bit-integer operations that are already available in C.
> 
> --  
> Lloyd Zusman  
> ljz@asfast.com  
> God bless you.

---

<div class="post-metadata">

**Author:** ![Aaron\_Patterson2](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Aaron\_Patterson2](https://rubytalk.org/u/Aaron_Patterson2)\
**Post date:** [4 June 2006 18:29 UTC](https://rubytalk.org/t/rubys-bignum/27961/9 "2006-06-04T18:29:15Z")

</div>

[snip]

> It's a nice excercise to get used to operators and numercial coercion. 🙂
> 
> Maybe we can even generalize that and put it into the standard lib -  
> something like a restricted integer that calculates with a max number  
> of bits.

I was hoping this would be in the standard lib since I have to do it a  
lot! I will be filling out that class so that I won't have to type as  
much though. 🙂

--Aaron

> **···**
>
> On Sun, Jun 04, 2006 at 06:32:54PM +0900, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 June 2006 16:55 UTC](https://rubytalk.org/t/rubys-bignum/27961/10 "2006-06-04T16:55:20Z")

</div>

Completely agree!

robert

> **···**
>
> 2006/6/4, Lloyd Zusman \<ljz@asfast.com\>:
> 
> > \> And if you need this in multiple places of the program then it's  
> > \> probably worth while to create a new subclass of Integer that  
> > \> restricts itself to 32 bit calculations
> 
> > \> It's a nice excercise to get used to operators and numercial coercion. 🙂  
> > \>  
> > \> Maybe we can even generalize that and put it into the standard lib -  
> > \> something like a restricted integer that calculates with a max number  
> > \> of bits.
> > 
> > This is indeed a good Ruby exercise, but if it ever ends up in the  
> > standard library, I would then vote for it to be re-implemented in C, as  
> > the Ruby version would be hugely less efficient than the native  
> > 32-bit-integer operations that are already available in C.
> 
> --  
> Have a look: [Robert K. | Flickr](http://www.flickr.com/photos/fussel-foto/)
