Count True Bits

mae

Member
Join Date
Feb 2007
Location
Arkansas
Posts
38
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 are true and write it to an integer. I don't guesss I have ever done this before because I am unable to come up with a way to do this. In the image that I have attached I would want to write a 5. Can someone please point me in the right direction!!

Thanks,
mae

B34 File.JPG
 
Do you want easy to program or easy to understand?

Easy to understand, and fastest to execute will be a brute force method.
First you zero out an integer word. Then for each bit you make a rung. You'll have 57 rungs.
XIC Bit Add N7:0 1 N7:0
Put it in a dedicated subroutine.

Or you can program a For/Next loop and use indirect addressing. This will take longer to execute but less time to program. Its also more difficult for some people to understand.

Search the forum for bit counting. This subject has been covered many times and there are some sample programs.
 
Last edited:
I've been in this same predicament. Unfortunately there's no elegant way to do it in RSLogix 500. I'd suggest TConnolly's brute force method--you shouldn't notice any effect on your scan time as you would with the looping.
 
A one shot could get you in trouble. What happens if B34/1 is toggling on/off and all the others are not changing?

Its more predictable to drop it all into a subroutine and just clear the count and then recount every scan.


Edit to add: the one shot is not likely to give you any performance boost, it is an extra instruction, an extra memory fetch, and an extra memory write per rung.
 
Last edited:
my apology for misinterperting you suggestion. After Rereading your first post and your further explaination in your second post now I understand. Thanks for the clairification.
 
All of this has been covered years ago on this forum. It is ancient knowledge that seems to be forgotten as soon as the thread drops off the front page.
These tricks have be known for ages. Chess programmers have been using them since the 1970s or earlier.

-1 for forum knowledge. What good does it do if we put so much effort into these threads just to have them ignored once the are scrolled off the front page.

From 5 years ago from a similar thread
http://www.plctalk.net/qanda/showpost.php?p=249240&postcount=13
 
@ sorenluder im pretty sure if you use Tconnolly's method, it should work. I don't think its gets any easier than that. You can't make an add on instruction for this because based on the B data files it is a logix 500 programming environment. Add on instructions started with logix 5000, unless ofcourse you want this done in logix 5000.
 
For those that want to do this in logix 5000 i have attached a sample program in structured text that i wrote quickly, i didn't check for bugs, but i think it should work. If you don't have logix 5000 then i have wrote the code below, Using a single For Loop and indirect addressing the code is much simplified see below:



//The Integer tag in this example is an Integer data type, and we are monitoring bits 0.....15, based on the # of bits that are on we sum them up and store
//our value to another integer tag called Bit_Count_Value which is initialized on everyscan


// Initialize
Bit_Count_Value := 0;


// Create a For Next loop to iterate through the Integer Tag
FOR Index := 0 TO

15 BY 1 DO

If Integer.[Index] THEN
Bit_Count_Value := (Bit_Count_Value + 1);

End_if;

END_FOR;
 
Geez - a bit count function would be handy! I use it all the time - one of my favourite functions.
 

Similar Topics

I have an array of 100 DINTS and I need to count the BITS that are True. I tried a nested FOR in Structured Text but it wouldn't allow the second...
Replies
40
Views
12,209
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,429
What is the raw count of Allen Bradley Flex Analog Output Module 5094-OF8 raw count?
Replies
1
Views
125
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
124
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
302
Back
Top Bottom