Hi all,
I am looking for an efficient method of getting the bits count of an integer class variable (i.e. how many bits in the variable are set "on").
So far I devised two ways, using just plain Basic code (I'm not familiar with Assembly language), and here is my benchmark code for both:
The second method seems to be slightly faster than the 1st one.
Any suggestion for a better way ?
I am looking for an efficient method of getting the bits count of an integer class variable (i.e. how many bits in the variable are set "on").
So far I devised two ways, using just plain Basic code (I'm not familiar with Assembly language), and here is my benchmark code for both:
Code:
[FONT="Courier New"] DEFLNG A-Z FUNCTION PBMAIN () AS LONG REGISTER NBITS AS LONG, NUM AS LONG '#REGISTER NONE N=10000000 T!=TIMER FOR I=1 TO N NBITS=0: NUM=I DO UNTIL NUM=0 NBITS=NBITS+(NUM AND 1) NUM=NUM\2 LOOP NEXT PRINT USING$("####.#",TIMER-T!) T!=TIMER FOR I=1 TO N NBITS=0 FOR NUM=0 TO 31 NBITS=NBITS+BIT(I, NUM) NEXT NEXT PRINT USING$("####.#",TIMER-T!) WAITKEY$ END FUNCTION [/FONT]
Any suggestion for a better way ?
Comment