Position of least significant bit that is set

后端 未结 23 1420
时光取名叫无心
时光取名叫无心 2020-11-22 08:46

I am looking for an efficient way to determine the position of the least significant bit that is set in an integer, e.g. for 0x0FF0 it would be 4.

A trivial impleme

23条回答
  •  感动是毒
    2020-11-22 08:55

    You could check if any of the lower order bits are set. If so then look at the lower order of the remaining bits. e.g.,:

    32bit int - check if any of the first 16 are set. If so, check if any of the first 8 are set. if so, ....

    if not, check if any of the upper 16 are set..

    Essentially it's binary search.

提交回复
热议问题