From: Eric Hodel Date: 2005-05-05T06:32:02+09:00 Subject: Re: Fixnum's binary representation On 04 May 2005, at 09:04, camsight@gmail.com wrote: > Hi, people! > > As far as I know, Ruby's Fixnum is 30-bit signed integer. > One bit is used as a flag for whether it's a direct value. > Another bit is used for non-Fixnum direct values. > > My guess is that Fixnum#[] works as if it's 32-bit signed integer. > It's not showing its real binary representation. > Is my guess true? $ ruby puts (-10..10).map { |i| "#{i}: #{i.object_id}" }.join("\n") -5: -9 -4: -7 -3: -5 -2: -3 -1: -1 0: 1 1: 3 2: 5 3: 7 4: 9 5: 11 A Fixnum's object_id is 2N+1 its value, so if you want a Fixnum's binary representation, use its object_id. (So long as the object_id is a Fixnum.) #define FIXNUM_FLAG 0x01 #define INT2FIX(i) ((VALUE)(((long)(i))<<1 | FIXNUM_FLAG)) Also, a Fixnum always has an odd object_id, while any other VALUE has an even object_id. -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04