Counting the number of bits that are set

后端 未结 3 756
悲&欢浪女
悲&欢浪女 2021-01-21 09:23

I want to count the number of bits in a binary number that are set. For example, user enter the number 97 which is 01100001 in binary. The program should give me that 3 bits are

3条回答
  •  自闭症患者
    2021-01-21 09:53

    The link Chris gave gives some good methods for bit counting. I would suggest this method, as it is both very fast and doesn't require looping but only bit-wise operation, which would be easier to do in assembly.

    Another way you might get the assembly code is to make the code in C, compile it and then look at the output assembly (most compiles can produce an assembly file output (-S for gcc, but make sure to disable optimization via -O0 to get easier to understand code), or allow you to view the binary file disassembled). It should point you in the right direction.

    As an anecdote, I've done some testing a while back on PowerPC (not MIPS, I know...) for the quickest way to count bits in a 32 bit int. The method I linked was the best by far from all other methods, until I did a byte sized lookup table and addressed it 4 times. It would seem that the ALU is slower than referencing the cache (running around a million numbers through the algorithm).

提交回复
热议问题