Announcement

Collapse
No announcement yet.

Convert word to bit index

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts


  • Convert word to bit index

    Hello,

    I have a screen on an HMI (C-more) that has a radio button. On the C-more, 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


  • #2
    you could use a For Loop and a bitshift right and compare the shifted value to get the bit position of the HI bit.
    Last edited by kewakl; 05-17-2019, 12:48 PM.

    Comment



    • #3
      Click image for larger version

Name:	bitposition.png
Views:	85
Size:	115.6 KB
ID:	122804

      Comment



      • #4
        Lookup Table can be used for this. Just use it as a single row instead of a matrix.
        Attached Files

        Comment



        • #5
          ^ I like and more characters to satisfy the forum overlords - have to have more characters per post, but must wait 1 minute between posts

          Comment



          • #6
            Does PxK not have ENCO???

            Or do it with logarithms. LOG2(register). You can do logs to arbitrary bases with any available log function to any base, with LOGa(b) = LOGc(b)/LOGc(a).

            So if you have ln or LOG10 as functions in your PLC, you can do LOG2(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.

            Comment



            • #7
              Originally posted by ControlsGuy View Post
              Does PxK not have ENCO???

              Or do it with logarithms. LOG2(register). You can do logs to arbitrary bases with any available log function to any base, with LOGa(b) = LOGc(b)/LOGc(a).

              So if you have ln or LOG10 as functions in your PLC, you can do LOG2(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.
              You know, sometimes I can't see the forest for the trees.

              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

              Comment



              • #8
                Intersting and useful idea using ln(). The only reason that you need to +1 it is due to PxK using 1-32 instead of 0-31 for its bit postions.


                Comment



                • #9
                  The only reason that you need to +1
                  It holds true in Vb and on my android phone calc, too. Not quite sure for C, haven't used it in a while.

                  Comment



                  • #10
                    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,2-31. To interpret as 1,2,3-32 as the op wants then yes the +1 is needed.

                    Comment

                    Working...
                    X