In my project I need to AND two binary array of size 40 bytes(320 bits) and then compute set bit count in C++. I found some algorithms to do this but I want to know what is the fastest way of implementing it in c++. I mean what c++ data type would be proper?(unsinged char*,unsigned int 32,u_int64,…). I know many algorithms are compatible with 32bit integer although my array size is 40 bytes .
what about the algorithms described in this link:
Fast Bit Counting Techniques which one is faster?
Is const type better or there is no difference?
Any help would be much appreciated.
Here’s a version that goes through the array with 4 bytes at once, requiring 10 iterations:
You can do this a lot faster with a modern CPU, using compiler intrinsics. For example on a 64 bit CPU with Visual C++:
But this is all with performance in mind, if you simply want some readable code that works definitely go with what Rob suggested.