Binaire maths question

"... without whom none of this would have been possible ... " !!

You're right Ken! I think like all good authors I'll have to include an "Acknowledgements" piece in the comment field in OB1!
 
Sorry for not knowing the level of expertise.....

...in this topic when I wrote my suggestion on how to solve the initial question.
It wasn't my intention to upset anyone, but as I saw it, my answer was actually the first suggestion that could be realized in the S7 environment.

And for your interrest I had the opportunity to check the source code for FC99 "BITSUM" and to my very surprise the algorithm used is proportional to n (number of bits). Imaging all the people that has used this function without actually knowing what they are using....

I also took the time to look through old threads in this topic and found the following link:

http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel

I spent some time on trying to implement the "best way" to count the number of bits set in a word but unfortunatley I didn't get it right.

******* From the link above ******
The best method for counting bits in a 32-bit integer v is the following:

unsigned int const w = v - ((v >> 1) & 0x55555555);
unsigned int const x = (w & 0x33333333) + ((w >> 2) & 0x33333333);
unsigned int const c = ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;

So now I would appreciate if someone, maybe the experts? can show me/us how this should be done? (Of course in STL for S7)

Bis Dann / Mårten
 
Please see below my tested implementation for finding the number of bits set in a 16 bit word. The algorithm you posted does not work in a S7-300 plc - I think this is due to the fact that an unsigned 32 bit multiply is not readily available (although I'm sure one could be coded). I did get the algorithm to work for a 16 bit variable (instead of 32) but I had to specifically check for the all ones case and I had to add an extra bit mask to the final result.

I tested this code by using the numbers 0000 through to $FFFF and compared the result produced by this code with the result produced by the Siemens block FC99 and made sure they were the same.

I also did a run time comparison (using the simulator) by calling this code 1000 times and calling FC99 1000 times for each increment and then summing the scan times. FC99 was taking around 5 times longer to execute by the time I got half way through the numbers. Hope this helps.

Code:
// if v <> 0xFFFF then 
// Begin
// wz = v - ((v >> 1) & 0x55555555);					// temp
// x = (wz & 0x33333333) + ((wz >> 2) & 0x33333333);	 // temp
// c = (((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24) & 0xf ; // count
// End
// Else c=16;
// EndIf
	 L	 DW#16#FFFF
	 L	 #v
	 ==I 
	 JCN okm
	 L	 16
	 JU	res
okm: SRD 1
	 AD	DW#16#55555555
	 L	 #v
	 TAK 
	 -D	
	 T	 #wz
	 SRD 2
	 AD	DW#16#33333333
	 T	 #dwtemp
	 L	 #wz
	 AD	DW#16#33333333
	 L	 #dwtemp
	 +D	
	 T	 #x
	 SRD 4
	 AD	DW#16#F0F0F0F
	 L	 #x
	 +D	
	 L	 DW#16#1010101
	 *D	
	 SRD 24
	 AD	DW#16#F
res: T	 #iResult
 
Good on you, we have raised the high bar

10baseT said:
Hold on a minute !! is no one else allowed a point of view on this ?




Sure, informed ones. Otherwise mediocrity or worse is perpetuated.

Actually , other people do know something about bit comparisons and manipulations




Not enough. Only a couple, but I can see there are more now.

Well done for thoroughly p*ssing people off with that attitude .




I admit I am being very hard a$$ed on this. I am just trying to raise the high bar a bit. Should I let it slide when I know better? Now others know better. I doubt they will let this slide now.



If you don't think there is noise on the forum then, check on this thread from two year ago.

http://www.plctalk.net/qanda/showpost.php?p=20205&postcount=10

I would say this thread got lost in the noise. Did I waste my time posting this? I think so because I had to repeat it.



Ken M said:
I looked at this at first and had no idea of it's relevance to the topic. Then I tried thinking (always recommended once in a while) and sketched a couple of numbers on a piece of paper and, click!, the light went on. I'm not entirely sure who to praise in this instance: it seems establishing the true provenance of a piece of code here can get a bit involved. But let's just say I would never have stumbled across this myself. Somethings you just have to be shown. And this so neat and elegant it should be framed and hung.



Ken M put forth the effort to understand. I think every one should rather than perpetuate mediocrity.


I can also see that MartAnd and SimonGoldsworthy have also done further research and have verified what I have been saying. I don't think MartAnd is pi$$ed but rather shocked that Siemens has mediocre code in their libraries. Now MartAnd, SimonGoldsworthy and others know better.

I am back.
 
Peter said:
...rather shocked that Siemens has mediocre code in their libraries
The mediocrity here may be intentional! This FC is provided in a folder named TI-S7 Conversion. I've no idea how the equivalent function in TI operated but I'll bet you this is a like-for-like conversion. There's a timer function in that library as well which is anything but elegant but it does replicate the way TI used to handle timers. My guess is some poor S7 software guy years ago was given a bunch of TI instructions and told 'convert them exactly'. And being Germanic he did just that - he didn't improve or optimise them for the S7 environment.

At least S7 gives the user the chance to roll his own and come up with a neater operation based on the discussions here.

Regards

Ken
 
I stole this from this site:
http://www.sjbaker.org/steve/software/cute_code.html
but it's pretty neat. It counts the number of bits in a double word (n):


Code:
n = (n & 0x55555555) + ((n & 0xaaaaaaaa) >> 1);
n = (n & 0x33333333) + ((n & 0xcccccccc) >> 2);
n = (n & 0x0f0f0f0f) + ((n & 0xf0f0f0f0) >> 4);
n = (n & 0x00ff00ff) + ((n & 0xff00ff00) >> 8);
n = (n & 0x0000ffff) + ((n & 0xffff0000) >> 16);
 
Here's the Step 7 implementation of the above for anyone that may need it.
Code:
FUNCTION "fcBitCountA" : INT
TITLE =
VERSION : 0.1

VAR_INPUT
  dwData : DWORD ;  
END_VAR
BEGIN
NETWORK
TITLE =calc number of bits set in 32 bit word
	  L	 #dwData; 
	  PUSH  ; 
	  AD	DW#16#55555555; 
	  TAK   ; 
	  AD	DW#16#AAAAAAAA; 
	  SRD   1; 
	  +D	; 
	  PUSH  ; 
	  AD	DW#16#33333333; 
	  TAK   ; 
	  AD	DW#16#CCCCCCCC; 
	  SRD   2; 
	  +D	; 
	  PUSH  ; 
	  AD	DW#16#F0F0F0F; 
	  TAK   ; 
	  AD	DW#16#F0F0F0F0; 
	  SRD   4; 
	  +D	; 
	  PUSH  ; 
	  AD	DW#16#FF00FF; 
	  TAK   ; 
	  AD	DW#16#FF00FF00; 
	  SRD   8; 
	  +D	; 
	  PUSH  ; 
	  AD	DW#16#FFFF; 
	  TAK   ; 
	  AD	DW#16#FFFF0000; 
	  SRD   16; 
	  +D	; 
	  T	 #RET_VAL; 
END_FUNCTION
 
DeHulk said:
I have a dataword ,each bit is actually an alarm, No i have to program a condition that shut down the installation when more than two bits are set at the same time.
I once saw a very short mathematical way of programming this but can't find it anymore .
Anyone has an idea ?
My regards
Looks like you've received plenty of possible solutions. Have you chosen any of them, and how did it work?
 
Plc S5 To S7

Hi
I have PLC S5 115uU and I try to use CPU S7 414 and S5 I/O rack. When I converter S5 program , i find some eroor for FB 12 ( FIFO ) and Fb 250 ( Read Analog input ) and FB 251 ( Write to analog Output).
Could you help me how can fixed these errors.
My email: [email protected]

Thanks
Albert



SimonGoldsworthy said:
Here's the Step 7 implementation of the above for anyone that may need it.
Code:
FUNCTION "fcBitCountA" : INT
TITLE =
VERSION : 0.1
 
VAR_INPUT
dwData : DWORD ; 
END_VAR
BEGIN
NETWORK
TITLE =calc number of bits set in 32 bit word
	 L	 #dwData; 
	 PUSH ; 
	 AD	DW#16#55555555; 
	 TAK ; 
	 AD	DW#16#AAAAAAAA; 
	 SRD 1; 
	 +D	; 
	 PUSH ; 
	 AD	DW#16#33333333; 
	 TAK ; 
	 AD	DW#16#CCCCCCCC; 
	 SRD 2; 
	 +D	; 
	 PUSH ; 
	 AD	DW#16#F0F0F0F; 
	 TAK ; 
	 AD	DW#16#F0F0F0F0; 
	 SRD 4; 
	 +D	; 
	 PUSH ; 
	 AD	DW#16#FF00FF; 
	 TAK ; 
	 AD	DW#16#FF00FF00; 
	 SRD 8; 
	 +D	; 
	 PUSH ; 
	 AD	DW#16#FFFF; 
	 TAK ; 
	 AD	DW#16#FFFF0000; 
	 SRD 16; 
	 +D	; 
	 T	 #RET_VAL; 
END_FUNCTION
 
Hi
I have PLC S5 115uU and I try to use CPU S7 414 and S5 I/O rack. When I converter S5 program , i find some eroor for FB 12 ( FIFO ) and Fb 250 ( Read Analog input ) and FB 251 ( Write to analog Output).
Could you help me how can fixed these errors.
My email: [email protected]

Thanks
Albert
 
Plc S5 To S7

Hi
I have PLC S5 115uU and I try to use CPU S7 414 and S5 I/O rack. When I converter S5 program , i find some eroor for FB 12 ( FIFO ) and Fb 250 ( Read Analog input ) and FB 251 ( Write to analog Output).
Could you help me how can fixed these errors.
My email: [email protected]

Thanks
Albert


Alaric said:
For all N>0, if N & (N-1) != 0, then at least two bits are set.

--------+---------+-
|SUB |
| N | //Subtract 1 from N
| 1 |
| A | //Store result in A
+---------+

--------+---------+-
|AND |
| N | //Bitwise And N and A
| A |
| B | //Store result in B
+---------+

ALARM
--+---------+---------{ )-
|NEQ |
| B | //If B != 0 then alarm
| 0 |
+---------+




------------------------------------------
edit:
Ooops, I should have read the post a little better, the OP was looking for MORE than two bits.

(N&(N-1)) & ((N&(N-1))-1)) != 0 should do the trick to test if three or more bits are set.



--------+---------+-
|SUB |
| N | //Subtract 1 from N
| 1 |
| A | //Store result in A
+---------+

--------+---------+-
|AND |
| N | //Bitwise And N and A
| A |
| B | //Store result in B
+---------+

--------+---------+-
|SUB |
| B | //Subtract 1 from B
| 1 |
| C | //Store result in C
+---------+

--------+---------+-
|AND |
| B | //Bitwise And B and C
| C |
| D | //Store result in D
+---------+
ALARM
--+---------+---------{ )-
|NEQ |
| D | //If D != 0 then Alarm
| 0 |
+---------+

 

Similar Topics

Hi Folks I hope this is not a dumb question,I am a newbie . I want to perform simple math functions like multiplication, addition etc in TIA...
Replies
6
Views
2,818
Hi all, I am trying to copy paste a routine from rs logix500 into rs logix5000, I get an error on rung where they are searching for scada values...
Replies
1
Views
1,853
Hi I am fairly new to PLC programming and I am self learning. I have written a simple program to detect and store the RPM and ultimately the...
Replies
0
Views
1,413
Hi All, Rockwell Compact Logix processor. I'm using an Add function that I trigger by a using an XIC instruction followed by a ONS. The add...
Replies
6
Views
2,108
Hi everyone, I am generally comfortable with AB PLC's and programming but have recently acquaired a job where I need to modify an Omron Cqm1h...
Replies
2
Views
3,721
Back
Top Bottom