From: Rick DeNatale Date: 2006-10-26T10:55:56+09:00 Subject: Re: simple math question On 10/25/06, Jeffrey Schwab wrote: > Paul Lutus wrote: > > Brad Tilley wrote: > > > >> Thanks for all the examples guys! Being Ruby, I thought there might be > >> some super simple method that just magically did this. I'll stick with > >> the old-fashioned modulo test. > > > > If speed matters, the modulo test is way slower than testing the LSB. > > Do you mean on a specific architecture, or is there a Ruby-specific > reason for one to be slower than the other? Not really ruby-specific, and for the most part architecture independent. Modulo basically involves an integer division operation, whereas bit testing is a simple logical and with a mask. (int & 1).zero? is probably faster than int[0].zero? Since the latter generates the mask using the bit position. and both should be faster than (int % 2).zero? But it it's performance critical, benchmarking is recommended to verify. One should never simply take 'rules of thumb' for granted. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/