Given an unsigned number, what is a good (preferably fast) way of getting the number of leading 1-bits?
I need this to calculate the CIDR number from a quad-dotted IPv4 netmask.
Note: I have seen getting-the-number-of-trailing-1-bits, so I can do the table-based lookup.
Note 2: Even though I added a couple of language tags, for me it is about the algorithm, so if you have a good one in another language, please feel free to post.
Edit: on endian-ness.
I just found out that the INET_ADDR function and IN_ADDR structure store the IPv4 address in big-endian form, whereas the x86 is little-endian, and most of the processors I use are little-endian too.
So, lookup tables for this specific case are fast enough.
But thanks for the other answers: they work very nicely in the more common case.
--jeroen