TConnolly
Lifetime Supporting Member
Archie said:I was working on a message rotator in a SLC 5/04 when I came into a problem that seemed like it would be very easy, but turned out to be quite tricky in ladder.
I have a word (B3:0) that will have a single bit active and I need to find which corresponding bit number is active and store it in N7:0. For example
If B3:0 = 0000000000000001 , then N7:0 should be 0
If B3:0 = 0000000000000010 , then N7:0 should be 1
If B3:0 = 1000000000000000 , then N7:0 should be 15
A simple LOG base 2 would solve it easily, but I could only find a LN and LOG base 10 in the instruction set. I am thinking there is a very simple solution that evades me.
...... OK, just as I finished typing this I came up with the answer, but I would still be interested in seeing how others solve it.
I'm literally floored that no one has yet suggested that you use the ENC (encode) instruction. The instruction is available in RSLogix500 and it does exactly what you are describing.
But, since using the forum search has come up, I have posted on this topic several times and shown dirrerent ways of doing this and also what to do to make sure only one bit is set in the word.
To calculate log base two of a number use
X = LN(Y)/LN(2)
or save a step and use
X = LN(Y)/.69314718
However, that is a math intensive method.
But right here I showed another siimple way of computing log base 2 of an integer using a couple of bitwise AND and OR instructions.
http://www.plctalk.net/qanda/showpost.php?p=140998&postcount=6
Last edited: