Find nth SET bit in an int

后端 未结 11 965
栀梦
栀梦 2020-12-14 18:14

Instead of just the lowest set bit, I want to find the position of the nth lowest set bit. (I\'m NOT talking about value on the nt

11条回答
  •  北海茫月
    2020-12-14 18:40

    v-1 has a zero where v has its least significant "one" bit, while all more significant bits are the same. This leads to the following function:

    int ffsn(unsigned int v, int n) {
       for (int i=0; i

提交回复
热议问题