Hello,
I have a screen on an HMI (Cmore) that has a radio button. On the Cmore, the radio button allows the set value to be stored in a WORD tag.
So as the user selects the radio button, the WORD changes from 1,2,4,8, etc..
That's fine so far. What I'm wondering is if there is a way to convent that into incremental numbers that represent the index of the bit that is active within the WORD.
So,
1,2,4,8,16,32 becomes 1,2,3,4,5,6,7 etc.
I don't see an easy way to do this without creating a separate tag for each option.
Thanks
FF
Announcement
Collapse
No announcement yet.
Convert word to bit index
Collapse
X

Hmm i used windows calculator and did not need the +1 .....
Actually no, i misinterpreted. Without the +1 will give you bit position as in 0,1,231. To interpret as 1,2,332 as the op wants then yes the +1 is needed.

The only reason that you need to +1
Leave a comment:

Intersting and useful idea using ln(). The only reason that you need to +1 it is due to PxK using 132 instead of 031 for its bit postions.
Leave a comment:

Originally posted by ControlsGuy View PostDoes PxK not have ENCO???
Or do it with logarithms. LOG_{2}(register). You can do logs to arbitrary bases with any available log function to any base, with LOG_{a}(b) = LOG_{c}(b)/LOG_{c}(a).
So if you have ln or LOG_{10} as functions in your PLC, you can do LOG_{2}(register) as ln(register)/ln(2) or LOG(register)/LOG(2). THEN, note that ln(2) and LOG(2) are constants, so they just evaluate to k*ln(register) or k*LOG(register). You will have to range check to make sure the register isn't 0, and also distinguish between register values of 1 and 0.
I've used int(log(x)) + 1 in C/VB to find the SIZE (in digits) of a numeric!!. never thought to use it for bit position. Thanks.
based on controlsguy's post try this
( ln(bitweight) / ln(2) )+1
assume bitpos 7, weight 64
ln(64) / ln(2) = 6
add 1 = 7
assume bitpos 10, weight 512
ln(512) / ln(2) = 9
add 1 = 10
Leave a comment:

Does PxK not have ENCO???
Or do it with logarithms. LOG_{2}(register). You can do logs to arbitrary bases with any available log function to any base, with LOG_{a}(b) = LOG_{c}(b)/LOG_{c}(a).
So if you have ln or LOG_{10} as functions in your PLC, you can do LOG_{2}(register) as ln(register)/ln(2) or LOG(register)/LOG(2). THEN, note that ln(2) and LOG(2) are constants, so they just evaluate to k*ln(register) or k*LOG(register). You will have to range check to make sure the register isn't 0, and also distinguish between register values of 1 and 0.
 1 like
Leave a comment:

^ I like and more characters to satisfy the forum overlords  have to have more characters per post, but must wait 1 minute between posts
Leave a comment:

Lookup Table can be used for this. Just use it as a single row instead of a matrix.Attached Files
 2 likes
Leave a comment:
Leave a comment: