Daniel Martin wrote: > Ch Skilbeck <ruby / skilbeck.com> writes: > >> Can someone tell me if there's a better way to do this? It takes a >> number and returns the next power of 2 up (or the original if it was >> already a power of 2) Specifically, are there features of Ruby that I >> should be using in this case? > > Others have already given you floating point solutions to this, but > I've found that at least for numbers under 2**64, this is faster (uses > only integer arithmetic). This method is also easily translateable > into extremely fast C, if that becomes necessary: This is not because of the integer arithmetic, try this: def nextPowerOf2(n) 1 << (Math.log(n) * Math.log(2)).ceil end > [...snip...] cheers Simon