How to Count true bits in an array of DINTS

Results...

I put the Stanford approach (Jeremy's HammingWeight) into a loop and also created a different very simple loop.

Initially with only a few bits set in the array the simple loop was much faster, 700uS vs 4400uS.
However with some bits true in ALL of the 100 Elements, the Simple loop was MUCH Slower, 9900uS vs 4500uS

So I added the If X > 0 test to the Stanford Method and got better results.
The Stanford Method never exceeds 4600uS where as the Loop method will hit 10000uS if all Dints have at least one bit true.
Furthermore the Stanford method so far is at least 100uS faster that the loop method in any test I created and gets WAY better above 4400uS if most of the bits are set.

So I think this sums all this up. Thank you all for the help.

Code:
/* Simple Loop */
X:=0;
Z:=0;
BitCount :=0;
FOR X := 0 to 99 DO
    IF (Alarm_Dint[X] > 0) THEN
        FOR Y := 0 to 31 DO
            IF Alarm_Dint[X].[Y] THEN
                Z := Z+1;
            END_IF;
        END_FOR;
    END_IF;
END_FOR;
BitCount:=Z;
Code:
/* Stanford approach (HammingWeight) */
X :=0;
SumOfBits :=0;[INDENT] For X:= 0 to 99 do[INDENT]IF Alarm_Dint[X] >0 THEN
    Word :=Alarm_Dint[X];
    Word := Word - ((Word / 2) & 16#55555555);
    Word := (Word & 16#33333333) + ((Word / 4) & 16#33333333);
    Word := (Word + (Word / 16)) & 16#0F0F0F0F;
    Word := Word + (Word / 256);
    Word := Word + (Word / 65536);
    Word := Word & 16#0000003F;
    SumOfBits := SumOfBits + Word;
END_IF;
[/INDENT]End_For;[/INDENT]
 
What controller was your test on? 4.4ms is curious.

I had to substitute divisions in this algorithm for their 'equivalent' bit-shift operation - for some reason Rockwell doesn't support bit-shifting in Structured Text and I don't know whether their compiler is smart enough to substitute a machine code bit-shift when it encounters divide/multiply by powers of two, much less whether this substitution happens depending on the target CPU support.
 
Last edited:
Your simple loop example is going to have an issue if the most significant bit is set. The Logix platform won't perform unsigned integer operations.

Keith
 
I noticed that when I was doing some other testing. I could work around it by not using the highest bit. It's not ideal but I don't see any other way. Since its slower and the other process works well it doesn't really matter much at this point. Do you have a different idea to solve it in that case?

I'll start another thread looking for a way to detect ONLY true transitions of this array. I thought counting them would help, but it turns out it doesn't help as much as I hoped.
 
Last edited:
Maybe you'll like this

Do you have a different idea to solve it in that case?
I tinkered around with my logic I posted. I didn't care for the clunky approach I'd used to handle negative numbers. I settled on this: If the argument is negative, clear the high (sign) bit and add 1 to the result and proceed as with a positive number.
 
I tinkered around with my logic I posted. I didn't care for the clunky approach I'd used to handle negative numbers. I settled on this: If the argument is negative, clear the high (sign) bit and add 1 to the result and proceed as with a positive number.
That seems complicated. Can't you simply compare if not equal to 0 instead of greater than?
 

Similar Topics

I am needing to count the number of true bits that are in a file. They start at B34/1 and end at B34/56. All I am trying to do is count how many...
Replies
19
Views
8,358
I am trying to make a function that count the number of True BOOL inn a specific Array and return the count to an INT AlarmArrayActiveAlarms. I...
Replies
8
Views
4,481
What is the raw count of Allen Bradley Flex Analog Output Module 5094-OF8 raw count?
Replies
1
Views
139
I am working on a Markem X40 printer which uses NGPCL Commands and needs to send commands using a .net program. I need help with the following...
Replies
1
Views
178
Hi, I'm programming in RSLogix 500, and I'm wondering how I would program a Jog command that does not increase the encoder count. Basically we'd...
Replies
3
Views
323
Back
Top Bottom