Submitted:
09 June 2025
Posted:
10 June 2025
You are already at the latest version
Abstract
Keywords:
1. Introduction
| S. No | Synonym | Contextual Description |
|---|---|---|
| 1 | Bit Count | Generic term for counting bits, often implies counting 1s |
| 2 | Population Count | Most widely used term in software/hardware literature |
| 3 | Hamming Weight | Term used in information theory and coding |
| 4 | 1’s Count | Number of 1s in the binary sequence |
| 5 | Set Bit Count | Bits that are 1 |
| 6 | Bitwise Weight | Usud in Hardware Literatures , specially related to FPGA |
2. Case Study
| Algorithm 1 Population count using Naive Bitwise Count |
|
def popcount_naive(NUM : int) -> int:
COUNT = 0
while NUM:
COUNT += NUM & 1
NUM = NUM >> 1
return COUNT
|
| Algorithm 2 Population count using Brian Kernighan’s Algorithm |
|
def popcount_b_kerninghan(NUM : int) -> int:
COUNT = 0
while NUM:
NUM = NUM & (NUM - 1)
COUNT += 1
return COUNT
|
| Algorithm 3 Population count using Lookup Table |
|
bit_Table_256 = [0] * 256
for bit_i in range(256):
bit_Table_256[bit_i] = (bit_i & 1) + bit_Table_256[bit_i >> 1]
def popcount_LookupTable(NUM : int) -> int :
return bit_Table_256[ NUM & 0xff ] + bit_Table_256[ (NUM >> 8) & 0xff ] +
bit_Table_256[ (NUM >> 16) & 0xff ] + bit_Table_256[ (NUM >> 24) & 0xff ]
|
| Algorithm 4 Population count using CSA - Carry Save Adder| |
|
from typing import List
def csa(a: int, b: int, c: int) -> (int, int):
total = a ⌃b ⌃c
carry = (a & b) | (b & c) | (a & c)
return carry,total
def popcount(x: int) -> int:
x = x - ((x >> 1) & 0x5555555555555555)
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F
x = x + (x >> 8)
x = x + (x >> 16)
x = x + (x >> 32)
return x & 0x7F
def popcount_csa(data: List[int]) -> int:
ones = twos = fours = eights = 0
i = 0
n = len(data)
while i + 7 < n:
t1, o1 = csa(data[i], data[i+1], data[i+2])
t2, o2 = csa(data[i+3], data[i+4], data[i+5])
f1, t3 = csa(t1, t2, data[i+6])
f2, ones = csa(o1, o2, data[i+7])
fours, twos = csa(f1, f2, twos)
eights, fours = csa(eights, fours, 0)
i += 8
TOTAL = (
8 * popcount(eights) +
4 * popcount(fours) +
2 * popcount(twos) +
1 * popcount(ones) )
while i < n:
TOTAL += popcount(data[i])
i += 1
return TOTAL
|
| Algorithm 5 Population count using VTPC method |
|
def popcount_VTPC(n : int) -> int :
mask_size = 4
mask = [0,1,1,2]
m = 0
i = 0
e = mask_size
r = n
while d ≥ 0 :
return m
|
| Algorithm 6 Population count using Divide and Conquer adder |
|
def popcount_divconq(NUM : int) -> int :
NUM = (NUM & 0x55555555) + ((NUM >> 1) & 0x55555555)
NUM = (NUM & 0x33333333) + ((NUM >> 2) & 0x33333333)
NUM = (NUM + (NUM >> 4)) & 0x0F0F0F0F
NUM = (NUM * 0x01010101) >> 24
return NUM
|
| Algorithm 7 Population count using RF method |
|
def countRF(N):
TEMP = N
COIN = 2
prev = 1
COUNTER = 0
while TEMP :
ss = (N+1)//COIN
v1 = (N+1)%COIN
if v1 <= prev:
REM = 0
else:
REM = (N+1)%prev
COUNTER += REM + ss*prev
prev = prev<<1
COIN = COIN<<1
TEMP = TEMP >> 1
return COUNTER
def RF(N):
return countRF(N) - countRF(N-1)
|
3. Complexity Comparison
| S.No | Old parameter | Transformed parameter |
|---|---|---|
| 1 | N | Log10 N |
| 2 | T | Ln T |
| S. No | Method | Average Time | Space |
|---|---|---|---|
| Complexity | Complexity | ||
| 1 | Naive Approach | O(M) | O(1) |
| 2 | Brian Kerninghan | O( (N)) | O(1) |
| 3 | Lookup Table | O(M / L) | O(256) |
| 4 | CSA | O( (M)) | O(M) |
| 5 | VTPC | O( (N)) | O(S) |
| 6 | Divide and Conquer | O( (M)) | O(1) |
| 7 | RF Algorithm | O(log2 N) | O(1) |

| N | log10N | NAIVE | KERNINGHAN | LOOKUP | CSA | RF | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| T | Ln T | T | Ln T | T | Ln T | T | Ln T | T | Ln T | ||
| 1 | 0 | 0.009 | -4.711 | 0.009 | -4.711 | 0.008 | -4.828 | 0.016 | -4.135 | 0.01 | -4.605 |
| 10 | 1 | 0.011 | -4.51 | 0.009 | -4.711 | 0.01 | -4.605 | 0.037 | -3.297 | 0.029 | -3.54 |
| 100 | 2 | 0.084 | -2.477 | 0.051 | -2.976 | 0.07 | -2.659 | 0.409 | -0.894 | 0.426 | -0.853 |
| 103 | 3 | 1.206 | 0.187 | 0.776 | -0.254 | 0.389 | -0.944 | 3.403 | 1.225 | 5.509 | 1.706 |
| 104 | 4 | 15.816 | 2.761 | 12.887 | 2.556 | 4.616 | 1.53 | 22.309 | 3.105 | 62.769 | 4.139 |
| 105 | 5 | 227.941 | 5.429 | 130.344 | 4.87 | 29.188 | 3.374 | 228.655 | 5.432 | 886.574 | 6.787 |
| 106 | 6 | 1823.992 | 7.509 | 1858.631 | 7.528 | 281.325 | 5.64 | 3518.617 | 8.166 | 12526.57 | 9.436 |
4. Conclusions
References
- Lehmer, D. The machine tools of combinatorics, Applied Combinatorial Mathematics, EF Beckenbach, ed, 1964.
- Berkovich, S.; Lapir, G.M.; Mack, M. A bit-counting algorithm using the frequency division principle. Software: Practice and Experience 2000, 30, 1531–1540. [Google Scholar] [CrossRef]
- El-Qawasmeh, E. Beating the popcount. International Journal of Information Technology 2003, 9, 1–18. [Google Scholar]
- El-Qawasmeh, E.; Strauss, M.; Mack, M.; Berkovich, S. Increasing the efficiency of bit-counting. International Journal of Computers and Applications 2007, 29, 51–58. [Google Scholar] [CrossRef]
- Berkovich, S.; El-Qawasmeh, E.; Lapir, G.; Mack, M.; Zincke, C. Organization of near matching in bit attribute matrix applied to associative access methods in information retrieval. In Proceedings of the APPLIED INFORMATICS-PROCEEDINGS-; 1998; pp. 62–64. [Google Scholar]
- Clausecker, R.; Lemire, D.; Schintke, F. Faster Positional-Population Counts for AVX2, AVX-512, and ASIMD. arXiv 2024, arXiv:2412.16370 2024. [Google Scholar]
- Muła, W.; Kurz, N.; Lemire, D. Faster population counts using AVX2 instructions. The Computer Journal 2018, 61, 111–120. [Google Scholar] [CrossRef]
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2025 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).
