Position of least significant bit that is set

后端 未结 23 953
时光取名叫无心
时光取名叫无心 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 09:19

    recently I see that singapore's premier posted a program he wrote on facebook, there is one line to mention it..

    The logic is simply "value & -value", suppose you have 0x0FF0, then, 0FF0 & (F00F+1) , which equals 0x0010, that means the lowest 1 is in the 4th bit.. :)

提交回复
热议问题