andy> I wrote the following code to create a BitVector type as an extension
andy> some time ago.  It was a quick hack, and I make no guarantees
andy> that it even works with the latest verions...

I wrote too.

  --> http://www2a.biglobe.ne.jp/~seki/ruby/bits.rb


class Bits
  def initialize(n)
    @size = n
    @bits = "\0" * ((n/8)+1)
  end
  public
  attr_reader(:size)
  def lengh
    size
  end

  private
  def idx(i)
    raise "index" if (i >= size) or (i < 0)
    return i/8, (1<<(i%8))
  end
  
  public
  def set(i)
    bytes, bits = idx(i)
    c = @bits[bytes]
    @bits[bytes] = c | bits
  end

  public
  def reset(i)
    bytes, bits = idx(i)
    c = @bits[bytes]
    @bits[bytes] = c & (~bits)
  end

  public
  def set?(i)
    bytes, bits = idx(i)
    c = @bits[bytes]
    c & bits != 0
  end

  public
  def to_i
    sum = 0
    m = 1
    @bits.each_byte do |e|
      sum += e * m
      m = m << 8
    end
    sum
  end
end

if __FILE__ == $0
  sz = ARGV[0] ? ARGV.shift.to_i : 1024 * 1024

  b = Bits.new(sz)
  sz.times do |i|
    b.set(i) if i % 2 > 0
  end
  
  (sz-1).downto(0) do |i|
    print (b.set?(i)? "1" : "0")
  end
  puts
  
  puts
  puts sprintf("%b", b.to_i)
  puts b.to_i
end