Quickest way to get highest set bit on port in to a variable?


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by towlerg View Post
    Henric, never the less, not as quick as 8 rotates and 8 jump on carry in-line. Worst case, with no bits set in the target thats only 16 cycles.

    rscor01, I didn't realize LOOKUP would do that. How would that go?

    George
    lookdown2 index, >= [%00000000,%00000001,%00000010],temp
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by Archangel View Post
    lookdown2 index, >= [%00000000,%00000001,%00000010],temp
    Nice! Very clever indeed . I have never used this command before, but now I can see it's potential.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  3. #3
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    I may be wrong but I don't think this will work.
    LOOKDOWN2 Search,{Test}[Value{,Value...}],Var

    Lookdown2 compares the value of "Search" with the values in the list from left to right (index values 0 up to 255) with the "Test" comparison.

    So if "Search" = 5 and "Test" = "=>" then using your values for the Value in the list, the logic tests should look like this.
    (Values: 0, 1, 2, 4, 8, 16, 32, 64, 128) bits 0-7
    Test 1: is 5 => 0? : Yes, (0 will be stored in Var)
    The testing would stop right here for every value of "Search" (0-255) for bytes (0-65535) for words because the "Search" value will always be greater than or equal 0 (unless you're using Long variables)

    Maybe I'm wrong?

    As I see it, to use Lookdown2 you would still need to enter all the values from 0 to 255 inclusive into the list.
    And "Test" would need to be set to "="
    Lookdown2 supports up to 85 values in the list or up to 256 when using a PIC18.
    Last edited by Tabsoft; - 4th March 2015 at 02:03.
    Regards,
    TABSoft

  4. #4
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by Tabsoft View Post
    I may be wrong but I don't think this will work.
    LOOKDOWN2 Search,{Test}[Value{,Value...}],Var

    Lookdown2 compares the value of Search with the values in the list from left to right (index values 0 up to 255) with the "Test" comparison.

    So if Search = 5 and Test = "=>" then using your values for the Value in the list, the logic tests should look like this.
    (Values: 0, 1, 2, 4, 8, 16, 32, 64, 128)
    Test 1: is 5 => 0? : Yes, (0 will be stored in Var)
    The testing would stop right here for every value of Search (0-255) for bytes (0-65535) for words.

    Maybe I'm wrong?

    As I see it, to use Lookdown2 you would still need to enter all the values from 0 to 255 inclusive into the list.
    And
    Lookdown2 supports up to 85 values in the list or up to 256 when using a PIC18.
    No, I don't think that you are wrong. I think there is a minor change that needs to be done to Dave's code,

    Code:
    lookdown2 index, <= [%00000000,%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000,255],temp
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  5. #5
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Thanks for the feedback.

    I still think there are issues with this approach.
    I ran a test and printed the output.
    I ran a For/Next loop from 0 to 255.
    e.g.
    For i = 0 to 255
    Lookdown2 i, <= [%00000000,%00000001,%00000010,%00000100,%00001000, %00010000,%00100000,%01000000,%10000000,%11111111], j
    Next i

    The output resulted in 247 wrong values out of 256.

    I am attaching the output here.

    Again, perhaps I'm wrong and missing something?
    Lookdown2.pdf
    Regards,
    TABSoft

  6. #6
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by Tabsoft View Post
    Thanks for the feedback.

    I still think there are issues with this approach.
    I ran a test and printed the output.
    I ran a For/Next loop from 0 to 255.
    e.g.
    For i = 0 to 255
    Lookdown2 i, <= [%00000000,%00000001,%00000010,%00000100,%00001000, %00010000,%00100000,%01000000,%10000000,%11111111], j
    Next i

    The output resulted in 247 wrong values out of 256.

    I am attaching the output here.

    Again, perhaps I'm wrong and missing something?
    Attachment 7724
    Yes, you are right. We should always use truth tables just to make sure it is correct. We were just giving Matias a general idea. Try this now,

    Code:
    For i = 0 to 255
    Lookdown2 i, <= [%00000000,%00000001,%00000011,%00000111,%00001111, %00011111,%00111111,%01111111,%11111111], j
    Next i
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Thanks. That works great.

    To find the port pin state using this, you would just need to decrement the return value by 1 if the value is >0 ( same for NCD).

    When I get some time I will run this against NCD and check the instruction cycles to see which is quicker.

    Thanks again.
    Regards,
    TABSoft

Similar Threads

  1. Replies: 6
    Last Post: - 18th July 2012, 02:42
  2. Whats the quickest way to set bits?
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 26th October 2010, 04:38
  3. Bit set question
    By nverma in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd April 2007, 21:23
  4. set flags within a variable?
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 24th October 2006, 11:07
  5. How do I to set one bit in a register?
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th February 2006, 08:43

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts