S7 Finding first true bit in an array

Bratt

Lifetime Supporting Member
Join Date
Nov 2003
Location
West Sweden
Posts
726
I'am currently using a loop to find the first true bit in an array but this i pretty scan time consuming. So i was wondering if anyone know a better way to find the first true bit in an array of bools using Step7?
 
L D[AR2 said:
Find the first non-zero byte, then find the first non-zero bit in the byte.
Thanks i'll give it a try. I just had a feeling that i have seen a thread sometime ago describing an even better way but i cant find it. Maybe its was describing another scenario and thats why i cant find it.

Another question is it better for the scan time to do an add and check the status bit for non zero than using a comparator??
 
More tidbits

I would search find the first non zero dword. Which is the first? The most significant dword or the least significant dword.

Then I would find the the first word in the dword.
Then I would find the first byte in the dword.
Then I would rotate the byte left or right depending on whether I was look for the most significant or least significant bit set. I would do this by looking at the carry flag or the sign flag. I would not waste time shifting a mask and anding.
When you find the bit to you just want the bit mask or the bit index?
if the conversion from dword to real is fast then it may be faster to find the dword and convert it to a floating point number and then extract the bit number from the exponent using a shift and a subtract.

If you search for tidbits you can find more bit tricks.
 
Peter Nachtwey said:
I would search find the first non zero dword. Which is the first? The most significant dword or the least significant dword.
The least significant dword is the first

Peter Nachtwey said:
When you find the bit to you just want the bit mask or the bit index?
I want the index value. If the first bit is true i want the value zero so i can use it in a pointer later on

Peter Nachtwey said:
if the conversion from dword to real is fast then it may be faster to find the dword and convert it to a floating point number and then extract the bit number from the exponent using a shift and a subtract.
Does this work if more than one bit is true?
 
Or this implementation (from Peter's tidbits) could be used:

Code:
FUNCTION FC 4 : VOID
TITLE =
VERSION : 0.1

VAR_OUTPUT
  iBitOffset : INT ; 
  bFound : BOOL ; 
END_VAR
VAR_TEMP
  dwBits : DWORD ; 
  iBitNo : INT ; 
  dwData : DWORD ; 
END_VAR
BEGIN
NETWORK
TITLE =Find first bit set in 4 dwords starting at DB2.DBD0
	  L	 0; 
	  T	 #iBitNo; 
	  L	 DB2.DBD	0; 
	  L	 0; 
	  ==D   ; 
	  JCN   eval; 
	  L	 #iBitNo; 
	  +	 32; 
	  T	 #iBitNo; 
	  L	 DB2.DBD	4; 
	  L	 0; 
	  ==D   ; 
	  JCN   eval; 
	  L	 #iBitNo; 
	  +	 32; 
	  T	 #iBitNo; 
	  L	 DB2.DBD	8; 
	  L	 0; 
	  ==D   ; 
	  JCN   eval; 
	  L	 #iBitNo; 
	  +	 32; 
	  T	 #iBitNo; 
	  L	 DB2.DBD   12; 
	  L	 0; 
	  ==D   ; 
	  JCN   eval; 
	  JU	noev; 
eval: TAK   ; 
	  CAD   ; 
	  T	 #dwData; 
	  INVD  ; 
	  L	 1; 
	  +D	; 
	  L	 #dwData; 
	  AD	; 
	  T	 #dwBits; 
	  L	 DW#16#AAAAAAAA; 
	  AD	; 
	  L	 0; 
	  ==D   ; 
	  JC	p1; 
	  L	 #iBitNo; 
	  +	 1; 
	  T	 #iBitNo; 
p1:   L	 #dwBits; 
	  L	 DW#16#CCCCCCCC; 
	  AD	; 
	  L	 0; 
	  ==D   ; 
	  JC	p2; 
	  L	 #iBitNo; 
	  +	 2; 
	  T	 #iBitNo; 
p2:   L	 #dwBits; 
	  L	 DW#16#F0F0F0F0; 
	  AD	; 
	  L	 0; 
	  ==D   ; 
	  JC	p3; 
	  L	 #iBitNo; 
	  +	 4; 
	  T	 #iBitNo; 
p3:   L	 #dwBits; 
	  L	 DW#16#FF00FF00; 
	  AD	; 
	  L	 0; 
	  ==D   ; 
	  JC	p4; 
	  L	 #iBitNo; 
	  +	 8; 
	  T	 #iBitNo; 
p4:   L	 #dwBits; 
	  L	 DW#16#FFFF0000; 
	  AD	; 
	  L	 0; 
	  ==D   ; 
	  JC	p5; 
	  L	 #iBitNo; 
	  +	 16; 
	  T	 #iBitNo; 
p5:   SET   ; 
	  S	 #bFound; 
	  L	 #iBitNo; 
	  T	 #iBitOffset; 
	  JU	Exit; 
noev: SET   ; 
	  R	 #bFound; 
Exit: SET   ; 
	  SAVE  ; 
END_FUNCTION
 
Thank you all! You have once again proved that this site is great for seeking knowledge that is very hard to find elsewhere.

Now i have a couple of ideas and some code i'll try out the diffrent versions on monday in a real cpu to see the scan time differences
 
Last edited:
Bratt said:
Thank you all! You have once again proved that this site is great for seeking knowledge that is very hard to find elsewhere.
You might not find STL code elsewhere but believe me, this informaton is very old and can be found on the web in many locations.

It really bother me that those come here think the information doesn't exist if it isn't on the front page of a forum.
 
Peter Nachtwey said:
You might not find STL code elsewhere but believe me, this informaton is very old and can be found on the web in many locations.

It really bother me that those come here think the information doesn't exist if it isn't on the front page of a forum.

Believe me i tried both google and the search function on this page before i posted the question but i quess i searched for the wrong things. Therefore i was happy when i got some great answers and even some very useful code just hours after i posted the question.
 
OK, I will give you a break because your first language isn't English

However, you should google for
bit hacks
bit tricks
You bit bucket will runneth over.

You have to think like a geek and use the terms they would use. Back in the dark ages, before forums, before the internet, we had computer clubs and share these bit tricks.
 

Similar Topics

Hi all, I'm in the process of upgrading a PanelView1200 HMI program to be developed in FactroyTalk View Studio. The filetype for PanelView 1200...
Replies
7
Views
280
Hey all, pretty new to PLC and got a question regarding finding the MSB or the last non-zero bit in a SINT array in studio5000... I am reading...
Replies
2
Views
830
Having an issue connecting to my Micro820 PLC. I don't have an IP Explorer and I know its MAC Address is 5C:88:16:D8:E6:65. I'm connected to the...
Replies
5
Views
913
I have reached a dead end trying to find an EDS file. Manufacturer says to contact third party tech support. Clueless. RSLINX can see it, just...
Replies
9
Views
1,791
Hello, I have an array of 300 of UDT. In each UDT is an array of 3 DINT and another array of 3 REAL. I have 10 controllers that pull data from...
Replies
7
Views
1,160
Back
Top Bottom