Fastest way to count number of bit transitions in an unsigned int

前端 未结 6 2369
说谎
说谎 2021-02-09 20:31

I\'m looking for the fastest way of counting the number of bit transitions in an unsigned int.

If the int contains: 0b0000000000000000000000000000101

6条回答
  •  春和景丽
    2021-02-09 20:46

    Fastest depends on your scenario: As you specified your datatype as constant sized (unsigned int), it is possible with lookup table. But when you need this operation only once the constant overhead to init the table is too big, and scanning+counting through the int is far faster despite.

    I guess the overall best would be a combination: Look up table for a byte or word (256 or 64k entries is not so much), and then combine the bytes/words by their last/first bit.

提交回复
热议问题