Find most significant bit.

JeffKiper

Lifetime Supporting Member + Moderator
Join Date
Jun 2006
Location
Indiana
Posts
2,447
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
 
Last edited:
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?
 
Last edited:
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.
 
Last edited:
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.
 
Last edited:
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 have tested every screen and no single screen individually has this fault pop up, but when I compile and send to the PanelView it comes up. If I...
Replies
2
Views
27
Hi, One of my customers has an old fabric tensile testing machine. The IC # AD7501KN of its controller has malfunctioned. This IC is related to...
Replies
1
Views
62
Hello everyone, I am a student and would like to take the next step and learn FactoryTalk (Batch preferably) and how to create HMIs etc. Would...
Replies
4
Views
453
Hi, Have a look at this picture... How can I find out the memory address of this tag? It was created by adding it to DB "Data_block_1", but I...
Replies
6
Views
893
I've got TIA portal v18, KTP1200 HMI. I want to add a slider bar (if that's the correct name for it) to an HMI screen. Baslcally, like a...
Replies
8
Views
978
Back
Top Bottom