From: "Jesús Gabriel y Galán" Date: 2012-10-27T03:22:23+09:00 Subject: Re: bit count or bit set On Fri, Oct 26, 2012 at 7:34 PM, Charles Hixson wrote: > Kaspar Schiess wrote: >> >> Hei Charles, >> >> Any number of these bit twiddling hacks here: >> http://www-graphics.stanford.edu/~seander/bithacks.html >> >> will translate straight to Ruby. I guess the lookup table approach is >> easy to implement and reasonably fast. >> >> Another strategy would be to implement this in C and load it as an >> extension. Make sure this is really the hotspot of your code before >> optimizing... >> >> kaspar > > Right now I'm trying to do the simple optimizations that happen while > designing. The reason for the question is that I *don't* want to drop into > C, so I'm trying to design something that's "fast enough" in a higher level > language. And I'm skeptical about bithacks being very fast in a high level > language. They are a way to do something if you must, but they are really > MUCH more appropriate in C. Or C++, Ada, D, any of those. I'm not even > sure, though, that they are appropriate in Java. In Java one should > probably look for something that has been implemented in the JVM. There exist gems that implement a bitset in C. For example: https://github.com/tyler/bitset I haven't used any, but you might try some of them. Jesus.