Counting populated bits in an array, rslogix 5000, ST

jrwagner89

Member
Join Date
Nov 2012
Location
seattle
Posts
3
Hello! I have an application where I can only have 3 pumps running out of 6. I load the pumpRunning bits into an array. What I need to do is figure out how many of the array elements are populated. I could use the integer value of the array, but there are too many combinations of 3 pumps running to make that really feasible. Help me Obi-Wan PLC folks, you're my only hope.

Edit: Sorry, I didnt mention I need to do this in structured text.
 
Last edited:
Are you sure it's too many? 6 is only 64 combinations; an array of 64 INTs is all that is needed.


Or an array of 8 INTs is enough if you break it into two sets of three, with some extra code.


Or it could be done with a loop.


That said, it's only six of these: [if integer.BIT then bitcount := bitcount + 1; end_if;], plus one [bitcount := 0;] for initialization.


Caveat: the OP says the bits are in an array, but then talks about the "integer value of the array," which makes me assume the array of bits is actual an integer.


See also https://www.geeksforgeeks.org/count-set-bits-in-an-integer/


and https://www.plctalk.net/qanda/search.php?searchid=15799486


There is nothing new under the sun.
 
Last edited:
That said, it's only six of these: [if integer.BIT then bitcount := bitcount + 1; end_if;], plus one [bitcount := 0;] for initialization.
+1

If all you need is to count the number running then I don't think it'll get any easier or more straightforward then this.
 
Bit Hack

v = v - ((v >> 1) & 0x55555555); // reuse input v as temp
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count of bits

http://graphics.stanford.edu/~seander/bithacks.html

In Structured Text:
Calc := Count_Bits_in_This_Dint;
Calc := Calc - ((Calc/2) & 1431655765);
Calc := (Calc & 858993459) + ((Calc/4) & 858993459);
Count_of_Bits := ((Calc + (Calc/16) & 252645135) * 16843009) / 16777216;

For loop is easier to interpret:
Count:=0;
for Index:=0 to 31 do;
if DINT.[Index] then
Count:=Count+1;
end_if
end_for;
 
Last edited:
For that few, I might even forego the loop and just have six or seven lines of code. This will be more efficient and even easier for the viewer to understand.

EDIT: scratch this...I just saw the structured text requirement after posting...

pump_count.png
 
v = v - ((v >> 1) & 0x55555555); // reuse input v as temp
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count of bits

http://graphics.stanford.edu/~seander/bithacks.html

In Structured Text:
Calc := Count_Bits_in_This_Dint;
Calc := Calc - ((Calc/2) & 1431655765);
Calc := (Calc & 858993459) + ((Calc/4) & 858993459);
Count_of_Bits := ((Calc + (Calc/16) & 252645135) * 16843009) / 16777216;

For loop is easier to interpret:
Count:=0;
for Index:=0 to 31 do;
if DINT.[Index] then
Count:=Count+1;
end_if
end_for;
It is good that you remembered the bithacks site.
I don't like the ST example. There is a big difference between a divide and a shr() function when it comes to speed. Also, one must declare the data types correctly.


In this one example I would probably use the simple brute force method but if I was counting the number of white pawns on a chess board I would use the c version since checking 64 bits in a LWORD would be long.
 

Similar Topics

Hi Guys, Does anyone know a good way to count populated elements in an array? I have 2 arrays of 60 elements which hold strings. When a string...
Replies
29
Views
6,057
Studio 5000 v30.11 Good morning- Is there an easier way than FSC + CNT to tell how many elements of an array are populated and how many are empty...
Replies
14
Views
7,525
Hello I am looking for tips on how to count the duration of a given function and then how to display it on the hmi panel in the hh:mm:ss format...
Replies
4
Views
1,700
Guys, I know it will be silly but can't get my head around it. I have 3 conveyors, every one on a separate servo drive, and 2...
Replies
25
Views
3,498
The 1734-IB8 has no hardware counting function correct? I am trying to find something to substitute in for a 5069-IB16F (since lead times are...
Replies
3
Views
1,414
Back
Top Bottom