This technique is another way of counting bits; It does a bunch of clever bit tricks to do it in parallel in each int.
| 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. |
| 104 | int 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 | |
| 118 | int cRawBitArray::FindBit1(const int num_bits, const int start_pos) const |
| 119 | { |