From: Ara.T.Howard@... Date: 2005-05-05T03:04:49+09:00 Subject: Re: Fixnum's binary representation On Wed, 4 May 2005 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. > I wondered how Fixnum#[] works and tested it. > > def show_binary(i) > result = '' > 32.times do |b| > result = i[b].to_s + result > if (b % 8 == 7) and (b != 31) > result = " " + result > end > end > puts result + ": " + i.to_s > end > > show_binary(1) > show_binary(-1) > show_binary(2 ** 31 - 1) > show_binary((2 ** 31 - 1) * (-1)) > > Result: > > 00000000 00000000 00000000 00000001: 1 > 11111111 11111111 11111111 11111111: -1 > 01111111 11111111 11111111 11111111: 2147483647 > 10000000 00000000 00000000 00000001: -2147483647 don't deny yourself the joys of printf ;-) harp:~ > cat a.rb [ (1), (-1), (2 ** 31 - 1), ((2 ** 31 - 1) * (-1)) ].each{|n| printf "%32.32b\n", n} harp:~ > ruby a.rb 00000000000000000000000000000001 11111111111111111111111111111111 01111111111111111111111111111111 10000000000000000000000000000001 cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | renunciation is not getting rid of the things of this world, but accepting | that they pass away. --aitken roshi ===============================================================================