Bratt
Lifetime Supporting Member
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?
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.L D[AR2 said:Find the first non-zero byte, then find the first non-zero bit in the byte.
The least significant dword is the firstPeter Nachtwey said:I would search find the first non zero dword. Which is the first? The most significant dword or the least significant dword.
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 onPeter Nachtwey said:When you find the bit to you just want the bit mask or the bit index?
Does this work if more than one bit is true?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.
Yes, but it finds the most signficant bit. To find the index of the least signficant bit you must first strip all the most signficant bits away. This takes only a few instructions. Search for tidbits and myname.Bratt said:Does this work if more than one bit is true?
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
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.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.
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.