Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21096

[RESOLVED] Looking for possible optimization of code to count bits (1's).

$
0
0
I was wondering if anyone could find a better solution to this code i wrote.
The purpose of this function is to simply tell me how many bits are flagged in a long integer.
I figured there may be some sort of magical equation (which i am not currently seeing) that could possibly cut down most of the looping.
Thanks in advanced for any time you spend on this.

Code:

Public Function FindLetterCount(ByVal pValue As Long) As Byte
   
    Dim MyCnt As Byte
    Dim i As Long

    For i = 30 To 0 Step -1
        MyCnt = MyCnt + Abs(CBool(pValue And (2 ^ i)))
    Next i
   
    FindLetterCount = MyCnt

End Function


Viewing all articles
Browse latest Browse all 21096

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>