There is a neat trick in Knuth 7.1.3 where you multiply by a "magic" number (found by a brute-force search) that maps the first few bits of the number to a unique value for each position of the rightmost bit, and then you can use a small lookup table. Here is an implementation of that trick for 32-bit values, adapted from the nlopt library (MIT/expat licensed).
/* Return position (0, 1, ...) of rightmost (least-significant) one bit in n. * * This code uses a 32-bit version of algorithm to find the rightmost * one bit in Knuth, _The Art of Computer Programming_, volume 4A * (draft fascicle), section 7.1.3, "Bitwise tricks and * techniques." * * Assumes n has a 1 bit, i.e. n != 0 * */ static unsigned rightone32(uint32_t n) { const uint32_t a = 0x05f66a47; /* magic number, found by brute force */ static const unsigned decode[32] = { 0, 1, 2, 26, 23, 3, 15, 27, 24, 21, 19, 4, 12, 16, 28, 6, 31, 25, 22, 14, 20, 18, 11, 5, 30, 13, 17, 10, 29, 9, 8, 7 }; n = a * (n & (-n)); return decode[n >> 27]; }