udfCountSetBits
int udfCountSetBits (int)
int udfCountSetBitsR (int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfCountSetBits (intNumber)
intNumber = intNumber - ((intNumber >> 1) & 89478485)
intNumber = (intNumber & 858993459) + ((intNumber >> 2) & 858993459)
Return ((intNumber + (intNumber >> 4) & 252645135) * 16843009) >> 24
;..........................................................................................................................................
; DD.20080826. For 32-bit integer. Adapted from Sean Eron Anderson, seander@cs.stanford.edu.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfCountSetBitsR (intNumber) ; Recursive.
If !intNumber Then Return 0
If (intNumber & 1) Then Return 1 + udfCountSetBitsR (intNumber >> 1)
Else Return udfCountSetBitsR (intNumber >> 1)
;..........................................................................................................................................
; DD.20080826. For 32-bit integer.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

intNumber = 221 ; 6 bits are set = 11011101 = 128+64+16+8+4+1.

intCountBits1 = udfCountSetBits (intNumber) ; 6.

intCountBits2 = udfCountSetBitsR (intNumber) ; 6.

Exit