OR every bit position in array

bnrstnr

Member
Join Date
Apr 2011
Location
MI
Posts
3
I have an array of DINT and I want to turn a coil on if bit position 0 of any of the array is on, and so on.

So in plain text:

ArrayBit: DINT;
Array: ARRAY[32] of DINT;

IF Array[0].0 OR Array[1].0 OR Array[2].0 ...(through Array[32]... THEN ArrayBit.0 = TRUE

And I need to do that for every bit of the DINT[32]. Obviously if I typed it all out it would be insanely long.

Doesn't really matter if it's done in ladder or structured text. Sorry in advance for this ridiculous request :D I'm working on a Rexroth MLC if it matters.
 
Assuming CLX:

I'd clear a DINT then OR the entire array into that DINT using an FAL. If bit [0] of the result is non-zero...
 
I'm actually using IndraWorks, it's based on Codesys. I don't believe there is an equivalent function block to the CLX FAL. I'm fairly new to this platform, so there may be something that does the same thing that I'm not aware of.
 
If bit zero of any DINT is on, then the number will be odd...you could try a MOD 2 instruction?

Wrap it in a "for each" command or similar and you could again do it all in one "line"
 
I've used dwords instead of dints to minimise the type changing, but here's the code;

Code:
dwArrayBit:=dw#16#0;
For i:=1 to 32 do
 dwArrayBit:=dwArrayBit OR dwArray[i];
end_for;
 
counter1=0
counter2=0
do
if array[counter2].counter1= TRUE then arraybit.counter1=true
counter2=31; or use break(so many languages sorry)
end_if
if counter2=31 then counter1 +=
while counter1< 32
return
this is fastest way without using pointers etc.
 
If bit zero of any DINT is on, then the number will be odd...you could try a MOD 2 instruction?

Wrap it in a "for each" command or similar and you could again do it all in one "line"

MOD is a much more expensive instruction than bitwise operations.

operand AND 0x00000001 indicates an odd number.
 
Last edited:
counter1=0
counter2=0
do
if array[counter2].counter1= TRUE then arraybit.counter1=true
counter2=31; or use break(so many languages sorry)
end_if
if counter2=31 then counter1 +=
while counter1< 32
return
this is fastest way without using pointers etc.

assuming int array[32]
Code:
int test = 0

for(int i = 0; i < 32; i++)
{
   test = array[i] & 1
   if(test)
      break;
}
...

is test 0 or 1?
 
Last edited:
ladder makes the problem interesting: should this take 32 scan cycles? or should we perform 32 tests per scan?
 
Last edited:
ladder makes the problem interesting: should this take 32 scan cycles? or should we perform 32 tests per scan?

One rung with 32 branches, XIC on each, OTE at the end of the rung. I doubt you will get it done any faster or simpler than this. Sure, it would be ugly to look at on the monitor, but very easy to understand.
 
Since you are using Indraworks my first choice would be to do this:

Code:
PROGRAM PLC_PRG
VAR
	ArrayBit:	UDINT;
	ArrayBit2:	UDINT;
	MyArray:	ARRAY [0..31] OF UDINT;
	
	I:	        INT;
	
END_VAR

FOR i:= 0 TO 31 DO
	ArrayBit := ArrayBit OR MyArray[I];
END_FOR

Note: you can't have an array named array since that is a keyword in the language

There is the OR function block which can be extended to have the ridiculous number of 32 inputs like this:

attachment.php


bIGoR.jpg
 

Similar Topics

I want to know how to make a flip flop rung that will change states every scan. This is for a fast sequencer. Is there a special bit similar to a...
Replies
8
Views
2,175
Hello all! We have a plant in an area that is relatively prone to power outages. I am using RS Logix 5000 and there is no backup UPS that keeps...
Replies
8
Views
2,966
Hello Members: I want to ask you that is that is there any possibility to read every bit of a data type WORD in TwinCAT. I want to see condition...
Replies
2
Views
2,519
Is there a way to use the FAL instruction to do +=2 instead of +=1? I have an array that is organized with alternating "data" and "flag" values...
Replies
5
Views
123
Hi i would like to ask! Im using Omron CP1E PLC May i know how to use one input to trigger two outputs alternatively? Meaning press X0 on, Y0...
Replies
11
Views
393
Back
Top Bottom