MCPcopy Create free account
hub / github.com/devosoft/avida / CountBits2

Method CountBits2

avida-core/source/tools/cBitArray.cc:104–116  ·  view source on GitHub ↗

This technique is another way of counting bits; It does a bunch of clever bit tricks to do it in parallel in each int.

Source from the content-addressed store, hash-verified

102// This technique is another way of counting bits; It does a bunch of
103// clever bit tricks to do it in parallel in each int.
104int cRawBitArray::CountBits2(const int num_bits) const
105{
106 const int num_fields = GetNumFields(num_bits);
107 int bit_count = 0;
108
109 for (int i = 0; i < num_fields; i++) {
110 const int v = bit_fields[i];
111 unsigned int const t1 = v - ((v >> 1) & 0x55555555);
112 unsigned int const t2 = (t1 & 0x33333333) + ((t1 >> 2) & 0x33333333);
113 bit_count += ((t2 + (t2 >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
114 }
115 return bit_count;
116}
117
118int cRawBitArray::FindBit1(const int num_bits, const int start_pos) const
119{

Callers 3

operator<Method · 0.45
mainFunction · 0.45
RunTestsMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected