How to find the position of the only-set-bit in a 64-bit value using bit manipulation efficiently?

后端 未结 9 1710
有刺的猬
有刺的猬 2020-12-24 05:51

Just say I have a value of type uint64_t seen as sequence of octets (1 octet = 8-bit). The uint64_t value is known containing only one set bit<

9条回答
  •  -上瘾入骨i
    2020-12-24 06:30

    Modern hardware has specialized instructions for that (LZCNT, TZCNT on Intel processors).

    Most compilers have intrinsics to easily generate them. See the following wikipedia page.

提交回复
热议问题