Calculating Hamming weight efficiently in matlab

后端 未结 9 1529
你的背包
你的背包 2020-12-09 11:46

Given a MATLAB uint32 to be interpreted as a bit string, what is an efficient and concise way of counting how many nonzero bits are in the string?

I have a working

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 11:56

    I'm reviving an old thread here, but I ran across this problem and I wrote this little bit of code for it:

    distance = sum(bitget(bits, 1:32));
    

    Looks pretty concise, but I'm scared that bitget is implemented in O(n) bitshift operations. The code works for what I'm going, but my problem set doesn't rely on hamming weight.

提交回复
热议问题