Rx3i Count bits set in word

Ozpeter

Member
Join Date
Aug 2014
Location
Virginia
Posts
124
In a Rx3i what would be the most efficient way to count the number of bits set in a word variable?

The naive approach is to simply shift the bits out of the word and count the ones that are set. But is this the smartest way of doing it?

One smarter example is Brian Kernighan’s Algorithm which has the advantage that it only loops for the number of bits that are set in the number. And while I have no problem implementing it in ladder code I have no idea if this is the best way in an Rx3i.

So am I missing any tricks?
 
Last edited:
There is no single instruction in the Rx3i function set that will do it.
It will still be an iterative process, but there is a "bit position' instruction which returns the location of the first set bit in a word or double word. Combine that with a counter and a "bit clear" instruction and you can count them in the least number of iterations.

Locate the first true bit
Increment counter
Clear that bit
Repeat until all bits cleared (value = zero).
 
There is no single instruction in the Rx3i function set that will do it.
It will still be an iterative process, but there is a "bit position' instruction which returns the location of the first set bit in a word or double word. Combine that with a counter and a "bit clear" instruction and you can count them in the least number of iterations.

Locate the first true bit
Increment counter
Clear that bit
Repeat until all bits cleared (value = zero).

That's effectively the Kernighan algorithm. But implemented in a smart manner.

Thanks!
 

Similar Topics

I have a system using Rx3I CRU320 redundant CPU with Proficy Machine Edition Software. In the hardware configuration of each CPU module, under...
Replies
14
Views
384
Hi, we are using Rx3i CRU320 redundant PLC system and we noticed a discrepancy between Primary and Secondary controller. Couple of variables (DI)...
Replies
8
Views
278
Hi there, I'm doing some extensive testing and commissioning with a slew of new Emerson PACSystems RX3i PLCs. It would be convenient to...
Replies
5
Views
100
Hi there, Trying to get some ascii serial communications working via RS485 (COMMREQ functions). I have attached our wiring for the COM2...
Replies
1
Views
969
Hello all, First time poster, long time viewer of these forums. Could not find my solution on here. We have had issues with a Comm Fail on an...
Replies
2
Views
372
Back
Top Bottom