From: Joel VanderWerf Date: 2006-11-21T08:11:40+09:00 Subject: Re: Fast 32 bit ints in ruby ? devesh wrote: > Hi Folks, > > I want an efficient implementation of an Ipaddress class. The ones I > have seen (for eg: lib/ipaddr.rb) all represent things internally in > the form of a Bignum. And that is turning out to be a bottleneck for > me. Ideally I would want to create some kind of a class, in which I can > wrap in a normal C int and then operate with it directly. Does anyone > know of how to do this ?. Another way would be to create a struct > wrapping this int and then access that from my C extension (Like it is > done with the Jukebox example in the Pickaxe book). I am new to ruby > and to mixing C and ruby. What would be a good way to achieve the > following interface: What about packing the 4 bytes in a string: p "192.168.1.2".split(".").map{|d|Integer(d)}.pack("C*") # ==> "\300\250\001\002" Unfortunately, there's no fast way to do & on strings of bits in pure ruby, AFAIK. Now, that might be a nice extension: bit operations on strings, including reading and writing bits n1 through n2 as a substring of bits, & ^ | on substrings of bits, etc. Actually, maybe this will help: http://raa.ruby-lang.org/project/bitset/ never used it though... -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407