http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Bos) writes:

> Mark Dickinson <(E-Mail Removed)> wrote:

>

>> On Dec 4, 9:16=A0am, "Paul" <-> wrote:

>> > "sanjib" <(E-Mail Removed)> wrote in message

>> > > 1. What might be an efficient and fastest way to check set bits of an

>> > > integer ? Suppose I have one bit set out of 32 bits of an integer.

>> > > =A0 (I can think of K&R approach i.e. based on iterations which is

>> > > equal to the no of set bits in an integer.)

>> >

>> > If you definitely only have one bit set, you could try using a switch/

>> > case, and hope that the compiler makes it a fast one.

>>

>> From the evil tricks department, assuming you really do have exactly

>> one bit set: the values 1, 2, 4, ..., 1<<31 are all distinct modulo

>> 37, so simply reduce modulo 37 and then use a lookup table.

>

> That's a _very_ evil trick. I like it.
And, as it happens, if you need to do the same for 64 bits, the

smallest modulus is 67 -- the 7 making both 37 and 67 reasonably easy

to remember.

In case anyone cares, the array contents would be:

0, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,

4, 0, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55, 47,

5, 32, 0, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27, 29, 50,

43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56, 7, 48, 35,

6, 34, 33

--

Ben.