Question about Fixnum

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

On 4/14/07, huang zhimin [email protected] 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

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

On 4/13/07, huang zhimin [email protected] wrote:

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


[email protected]

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. Scroll
down to the secion on “Small Integers”.

Blessings,
TwP

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.

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.