From: camsight@... Date: 2005-05-05T01:04:50+09:00 Subject: Fixnum's binary representation 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 This is exactly how 32-bit signed integer's binary representations look like. 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? My another question is that even if (2 ** 31 - 1) is not a Fixnum, the above code works for it (Actually Bignum#[]). If the number is bigger than that, [] doesn't work. Fixnum#[] and Bignum#[] are cleverly hiding the internal facts and are made to simulate 32-bit signed integers? Thanks. Sam