Binaire maths question

juice76 said:
First, use a move instruction to copy the alarm word into another word. Let's call this word "ExamWord". Use a "Bit Shift" instruction to sequentially examine all the bits in the "ExamWord". When a bit has a value of 1, use a counter to count it. At the end of each word examination cycle (after you've looked at all the bits), reset the counter to 0 and move the alarm word into "ExamWord" again. If you count 3 bits or more in any examination cycle, set your shutdown bit.

To expand on this method, what if there were a way to only count the set bits and automatically skip over the clear bits? This way if there are only two set bits, we only loop twice. If there are four bits, we only loop four times.

While (ExamWord > 0) {
Examword := Examword & (ExamWord-1)
Count := Count + 1
}



In AB LL:
----------------------+----------+-
|MOV |
| N |
| ExamWord |
+----------+

----------------------+----------+-
|MOV |
| 0 |
| Count |
+----------+

-[LBL 1]-+----------+----+--+------------+-
|GRT | | |SUB |
| ExamWord | | | ExamWord |
| 0 | | | 1 |
+----------+ | | A |
| +------------+
|
+--+------------+--
| |AND |
| | ExamWord |
| | A |
| | ExamWord |
| +------------|
|
+--+------------+--
| |ADD |
| | Count |
| | 1 |
| | Count |
| +------------|
|
+--------<JMP 1>----



Logic can also be added for this app so that you stop looping when count=3.
 
Unless you want to use FC99 (BitSum).....

....you could use something like the following code to count the nbr of bits set in a Word:

IN: AlarmWord (Word)
OUT: NbrOfAlarms (INT)

//*** Reset Nbr of Alarms ***
L 0
T #NbrOfAlm
L P##AlarmWord
LAR1
//*** Load Nbr of bits in a Word ***
L 16
NEXT: T LW 0
A [AR1,P#0.0]
JCN _001
L #NbrOfAlm
L 1
+I
T #NbrOfAlm
_001: NOP 0
//*** Add offset to AR1 ***
L P#0.1
+AR1
L LW 0
LOOP NEXT

Regards MĂĄrten
icon14.gif
 
Hi

Here is "quick" tested function for your purpose. This source-code includes OB1 and FC170 which is "ALARM BIT COUNTER".
I used the same system like MartAnd.

My thumbs are middle of hand, so I cannot attach the picture of it.
I increased the picture inside the zip-file.


- Pete -
 
Last edited:
[FIN]Pete improved a little bit MartAnd's code.
So I dare to improve his code a little bit as well. :D

 
LAR1 P##AlarmWord
L 0 //init 1s counter
L 16 //init loop counter
SET //just in case
NEXT: A [AR1,P#0.0]
JCN NO_1
TAK //1s counter -> ACCU1
+ 1
TAK //loop counter -> ACCU1
NO_1: +AR1 P#0.1
LOOP NEXT
TAK
T #NbrOfAlm

 
Pete improved a little bit MartAnd's code.
So I dare to improve his code a little bit as well. :D

I'am really sorry but I didn't know anything about MartAnd's code before I improved my own. I trying to explain, I don't want to steal anybody's ideas/glory and use it like "I have improved it".
But anyway -> who cares? :D

Good work MartAnd! 🍻

-Pete-
 
Why optimize inferior algorithms?

MartAnd and Jacekd, why bother? Look at what Alaric and I have posted on bit manipulation. Try to understand these algorithms that are proportional to log2(n) because they are FAR superior to the algorithm that you posted which is proportional to n.

It makes me cringe when you post algorithms such as your obvious one because it raises the noise level and makes it harder for the rookies to find the good stuff.

I will write more about this when I get back home.
 
Hold on a minute !! is no one else allowed a point of view on this ? Actually , other people do know something about bit comparisons and manipulations - but there is not a lot of point in posting if you just consider it as noise -
Well done for thoroughly p*ssing people off with that attitude .
 
Just catching up on this thread, but I think there have been two really excellent answers.

One was the really beautiful bit in Alaric's -

While (ExamWord > 0) {
Examword := Examword & (ExamWord-1)
Count := Count + 1
}


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.

The second good answer I thought was Roy's. The original poster wanted his problem solved. Roy told him where to find the standard FC he already has in his library that solves his problem. Let's not get too hung up on the methodologies and efficiencies and forget that the whole aim is presumably to fix a problem.

Regards

Ken
 
Yes , no detraction for the beautiful simplicity of it but I think you can simplify it even further - though there is no reason to post something so simple ! what is nice about alerics solution is that the count is easy to change , with my more simple solution , it is a little more complicated to change the number of bits to be examined - tip , there is no need for a loop , and it can be done in one instruction.
 
Here's my contribution to the discussion. I used the Step 7 JL instruction to directly decode the number of bits set in a byte. The advantage of this method is that it always takes the same time to execute. If you got more than 8 bits, call it several times and sum the result.

Code:
	  L	 #byteBitPattern
	  JL	bert
	  JU	b0						  //0
	  JU	b1						  //1
	  JU	b1						  //2
	  JU	b2						  //3
	  JU	b1						  //4
	  JU	b2						  //5
	  JU	b2						  //6
	  JU	b3						  //7
//fill the rest in here...
bert: L	 8
	  JU	sum
b0:   L	 0
	  JU	sum
b1:   L	 1
	  JU	sum
b2:   L	 2
	  JU	sum
b3:   L	 3
	  JU	sum
b4:   L	 4
	  JU	sum
b5:   L	 5
	  JU	sum
b6:   L	 6
	  JU	sum
b7:   L	 7
	  JU	sum
b8:   L	 8
	  JU	sum
sum:  T	 #iResult
 
I use the JL instruction quite a lot myself, but I must admit I didn't think of it in this case.

It would be intersting to see the operation time for each of the solutions. I wouldn't be surprised if the JL is right up there amongst the front runners (and FC99 trainling along well to the rear!)

If I get a chance sometime I might drag out the 300-Series Ops list and work it out. Won't be this week though, things have suddenly got a bit hectic. We're apparently getting a visit from the Minister-president of Saxony on the 22nd of Feb. + press, so they want him to start everything running by pressing a big red button for the cameras - this after he's cut the red and white band strung across the doors to the capacitor-bank hall - YUK!
 
My alternative to using the JL instruction is to use indirect addressing and a lookup table to determine the number of bits set in a byte. The DB is preloaded with the bit count values according to the index of the value. (You could extend this idea and use a larger DB full of bits preloaded with 0's and 1's that correspond to the logic function that you require using a word as the indirect address to the relevant bit)

Code:
[size=1][color=#0000ff]OPN[/color][/size][size=1][color=#000080]DB[/color][/size][size=1] 5; 
 
[/size][size=1][color=#0000ff]L[/color][/size][size=1] #byteBitPattern; 
 
[/size][size=1][color=#0000ff]SLD[/color][/size][size=1] 3; 
 
[/size][size=1][color=#0000ff]T[/color][/size][size=1] #dwBitPatternPtr; 
 
[/size][size=1][color=#0000ff]L[/color][/size][size=1][color=#408080]DBB[/color][/size][size=1] [#dwBitPatternPtr]; 
 
[/size][size=1][color=#0000ff]T[/color][/size][size=1] #iResult; 
 
[/size][size=1][color=#000080]

Code:
DATA_BLOCK[/color][/size][size=1][color=#000080]DB[/color][/size][size=1] 5
[/size][size=1][color=#000080]TITLE[/color][/size][size=1][color=#0000ff]=
 
[/color][/size][size=1][color=#000080]VERSION[/color][/size][size=1] : 0.1
 
 
 
[/size][size=1][color=#000080]STRUCT[/color][/size][size=1] 
 
BitsSetLookup : [/size][size=1][color=#000080]ARRAY[/color][/size][size=1] [0 .. 255 ] [/size][size=1][color=#000080]OF[/color][/size][size=1][color=#800040]BYTE[/color][/size][size=1] ; 
 
[/size][size=1][color=#000080]END_STRUCT[/color][/size][size=1] ; 
 
[/size][size=1][color=#000080]BEGIN
 
[/color][/size][size=1]BitsSetLookup[0] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#0; 
 
BitsSetLookup[1] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#1; 
 
BitsSetLookup[2] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#1; 
 
BitsSetLookup[3] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#2; 
 
BitsSetLookup[4] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#1; 
 
BitsSetLookup[5] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#2; 
 
BitsSetLookup[6] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#2; 
 
BitsSetLookup[7] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#3; 
 
BitsSetLookup[8] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#1; 
 
BitsSetLookup[9] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#2; 
 
BitsSetLookup[10] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#2; 
 
BitsSetLookup[11] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#3; 
 
BitsSetLookup[12] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#2; 
 
BitsSetLookup[13] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#3; 
 
BitsSetLookup[14] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#3; 
 
BitsSetLookup[15] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#4; 
 
BitsSetLookup[16] :[/size][size=1][color=#0000ff]=[/color][/size][size=1] B#16#0; 
 
[/size]etc...
 
Hey Roy

So if we hear an enormous bang from a few hundred miles to the east the week after next, we'll know the system is well and truly up and running!
Make sure when you're speaking to the top man in Saxony you mention all our names "... without whom none of this would have been possible ... " !!

Good luck, fingers crossed

Ken
 

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,722
Back
Top Bottom