Are there any efficient bitwise operations I can do to get the number of set bits that an integer ends with? For example 1110 = 10112 would be two trailing 1 bits. 810 = 10002 would be 0 trailing 1 bits.
Is there a better algorithm for this than a linear search? I’m implementing a randomized skip list and using random numbers to determine the maximum level of an element when inserting it. I am dealing with 32 bit integers in C++.
Edit: assembler is out of the question, I’m interested in a pure C++ solution.
Taking the answer from Ignacio Vazquez-Abrams and completing it with the count rather than a table:
at the end b will contain the count of 1’s (the masks, adding and shifting count the 1’s).
Unless I goofed of course. Test before use.