Find most significant bit.

How do I find the most significant bit of a dint?

Backstory following.

I found out years ago that a small machine with 30 steps or less was easier to find the step if it was a bit of a word. We had guys moving steps by 5 into the step position. The maintenance guys had a hard time finding which MOV instruction was actually in command. So I started using a DINT and bits. Each bit is the step its in and you seal in the step so they are all on and you can see what its waiting on.


To trouble shoot 0000 0000 0000 0000 0000 0000 0001 1111

Bit 1 is step 1
Bit 2 is step 2
Bit 3 is step 3 and so on.

We just need to look at bit 6 of the word to see why its not stepping to the next step. I break the machine down into smaller steps if the process needs more than 30 steps. 0 is not running and 31 is complete.
I want to know the most significant bit so I can show it on an HMI. I don't care what the lesser bits are doing they have to be made for the most significant to make.
 
if you're trying to make it easy for the maintenance crew to follow, then something like the "brute force" method shown below is probably going to be the best bet ... if I understand your question - then this is about as simple to understand as it gets ...

just jump through this subroutine whenever you change to a new step ... the maintenance guys will be able to scroll down - looking for the first bit that's "green on the screen" - or even just look to see what value is currently stored in the HMI_Step_Display tag ...

note the RET (Return) instruction at the end of each rung ... that way - the first rung that tests TRUE will then kick the processor back out of the subroutine - so the rest of the rungs (those with less significant bit numbers) won't even have to be executed ...

and allow me to commend you for your interest in making the code easy for the maintenance crew to interpret ... many programmers these days don't even take that into consideration ...

I don't care what the lesser bits are doing they have to be made for the most significant to make.

along those lines, you might be able to make the current step number even easier to spot while troubleshooting if you could have only ONE of the step flag bits turned ON at a time ... naturally that depends on how you've already got your existing code configured ...

stay safe ... stay well ...
.

Step_To_HMI.PNG
 
I suppose another way is to use just an integer & forget about the bits, using an integer & move instructions means you do not have to worry about the size or looking at bits, also many HMI's can display text based on the value in an integer, for example "Step 22, Move packs to sealer", that way if it gets stuck then an engineer knows what are the problem is, providing the program is well documented then it would not be difficult to find the logic concerned.
I moved from using bits many years ago and never looked back. The answer is in the HMI message, i.e. the step number & what it should be doing, also in reality, you should be generating alarms i.e. "motor xxx overload transfer to sealer".
 
AB has a bit count AOI it's set up to count bits in a DINT[10] array, but you can modify the size if needed. I have attached if your interested.
 
Check if it is negative?

I may not be seeing something here - but can't you just check to see if it is less than zero if it is a signed DINT?

I interpreted your question as meaning you want the status of the most significant bit - which is bit 31.
I guess what you are asking is the most significant bit that is turned on?
 
Quote:
Originally Posted by chelton
Do you have ST available?
msb := abs(log(StepPos_DINT)/log(2))

+1


+2.


And let us not forget [ADD DINT_bits 1 REAL_temporary COP REAL_temporary DINT_temporary DIV DINT_temporary 16777216 DINT_temporary SUB DINT_temporary 127 high_bit]* ;)



Another way would be to set the "high_bit" integer to zero whenever the DINT_bits is zero (or to one when one, or [LES DINT_bits 2 MOV DINT_bits high_bit]), and then increment high_bit whenever you append a new bit to DINT_bits.


It could even be put into a routine**, called from every location that assigns a 1 to a in DINT_bits, so the same code is the only code used to append a bit and keep high_bit in sync.


* Might not be quite right in the details, and will need some special handling for -1, but the basic logic is correct.
** that either does a BSL to push a 1 bit at postion 0, or multiplies by 2 and adds 1, or does [ADD high_bit 1 high_bit OTE DINT_bits.[high_bit] - N.B. high_bit needs to start at -1 for that to work.
 
We use a lot of Red Lion screens and i can stack a display by order of creation. Least significant on bottom to most significant on top and only visible when high. The higher stacked display is displayed and covers the lower display.
 
Ron's method is the brute force method. There are better ways.


Ron's point, which he stated, was explicitly to have another priority i.e. above using a "better" algorithm.


I have written many lines of code, and in the end I learned the only thing that mattered (to others than me) was the UI. Yes I could have fun under the hood with cool algorithms and such, but no one else cared about anything other than the UI. I wrote a program with (what I considered) a very ugly, crummy UI because the sole user for that program know how to use that UI and starting over with a new UI would have been pointless.
 

Similar Topics

I'm not a controls guy, so go easy on wording. I'm building a lightweight Windows utility for engineering workstations and need to observe how...
9
Replies
416
Views
Hello Everyone, I have been programming a little while so I would of thought learning a new ladder program would not be to much of an issue, I...
29
Replies
871
Views
Genuine question, not a rhetorical one. I’m trying to understand how this is handled in practice. In Nov 2024 CISA published ICSA-24-326-02, an...
0
Replies
237
Views
Proface PFXSTM6400WADE (current model).. can find the hardware manual online, but what I really need is the operations manual, which I can’t find.
1
Replies
216
Views
Hello everyone, I am troubleshooting a Baumüller b maXX 4000 drive (BM4422) using ProDrive software. The machine occasionally reports an "Under...
5
Replies
602
Views
Top