# Question about Fixnum

**URL:** <https://www.ruby-forum.com/t/question-about-fixnum/91954>\
**Category:** Ruby\
**Created:** [April 13, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/question-about-fixnum/91954 "2007-04-13T19:09:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [April 13, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/question-about-fixnum/91954/1 "2007-04-13T19:09:56Z")

</div>

Why the range of Fixnum is -2^30 ~ 2^30 rather than -2^31 ~2^31 ?

---

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [April 13, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/question-about-fixnum/91954/2 "2007-04-13T19:12:57Z")

</div>

On 4/14/07, huang zhimin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why the range of Fixnum is -2^30 ~ 2^30 rather than -2^31 ~2^31 ?

```
                                                    ~~~~ should be

```

2^30-1 ~~~~ 2^31-1

–

---

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [April 13, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/question-about-fixnum/91954/3 "2007-04-13T19:33:59Z")

</div>

Because a bit is used to differentiate fixnums from other value types  
appearing in the same context, such as object pointers.

A regular Ruby object has its own memory space which Ruby interpreter  
has  
access via a pointer.  
Allocating memory for each number is obviously too expensive, so Matz  
decided to store “small” numbers in a pointer itself. This is Fixnum.  
This  
means you have to distinguish a fixnum from a pointer somehow. The bit  
I  
mentioned is used for the purpose. LSB of a fixnum is set to 1 because  
an  
object pointer’s LSB is guaranteed to be 0 on a 32bit/64bit system.

-Mak

---

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [April 13, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/question-about-fixnum/91954/4 "2007-04-13T19:36:59Z")

</div>

On 4/13/07, huang zhimin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why the range of Fixnum is -2^30 ~ 2^30 rather than -2^31 ~2^31 ?
> 
> –  
> removed\_email\_address@domain.invalid

Fixnum objects are immediate objects in the Ruy source code (other  
immediate objects are nil, true, false, and symbols). These immediate  
objects occupy 4 bytes of storage (unsigned long) on most machines.  
They are accessed by casting and bit shifting in the Ruby source code.

The other core types (String, Hash, Array, Object, Class, etc.) are  
structures and are accessed by pointer dereference.

The sneaky part is that all these objects are referenced as VALUE  
types in the source code. The allocated structures are 4-byte aligned  
such that the bottom two bits will always be zero. If bit zero is set  
to 1, then you have a Fixnum.

The consequence is that you have to right shift your Fixnum VALUE to  
get an integer, and hence, you loose one bit of precision for Fixnum.

Read more here … [http://rhg.rubyforge.org/chapter02.html](http://rhg.rubyforge.org/chapter02.html). Scroll  
down to the secion on “Small Integers”.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [April 13, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/question-about-fixnum/91954/5 "2007-04-13T20:32:02Z")

</div>

On Apr 13, 2007, at 1:36 PM, Tim P. wrote:

> Fixnum objects are immediate objects in the Ruy source code (other  
> immediate objects are nil, true, false, and symbols). These immediate  
> objects occupy 4 bytes of storage (unsigned long) on most machines.  
> They are accessed by casting and bit shifting in the Ruby source code.

As long as you think of this description (‘immediate objects’) as  
entirely an implementation issue, I think it is useful. When you  
switch to talking about Ruby language semantics, I think it is  
better to simply think of Fixnum objects and references as the same  
as all other Ruby objects and references. Same for NilClass,  
FalseClass, and TrueClass. The differences are that the language  
provides literal representations of these references  
(-1,0,1,nil,false,true) and also ensures that object identity and  
object equivalence are the same for their respective classes (for  
example, #dup has no meaning for these objects and you can’t create  
new instances of NilClass, FalseClass, and TrueClass).

Gary W.

---

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [April 14, 2007, 1:28am UTC](https://www.ruby-forum.com/t/question-about-fixnum/91954/6 "2007-04-14T01:28:36Z")

</div>

Hah! Much makes sense now. I was often curious as to why Fixnums’  
object IDs are always equal to 2\*n+1; the ID is also the storage  
mechanism. Thanks for this.

Harrison R.
