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
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    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

  2. #2
    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

  3. #3


    Did you find this post helpful? Yes | No

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

    Not wishing to be rude, but how can 255 iterations be quicker than NCD or the in-line assembler equivalent?

    George

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


    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
    Not wishing to be rude, but how can 255 iterations be quicker than NCD or the in-line assembler equivalent?

    George
    The 255 iterations is just to test the LOOKDOWN2 command line.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  5. #5
    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?

    As I see it, . . . . you only need 8 iterations with lookdown and using < or > as it jumps to the first "iteration" that tests as true.
    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.

  6. #6
    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?

    For those who are curios as to the testing results of NCD vs. Lookdown2 in the context of this discussion.
    I ran the following code in the MPLAB Simulator.

    Code:
    NCDSUB:
        for i = 0 to 255
    	mystatus = i
    	''Start MPLAB SIM Stopwatch
        	mybyte = NCD mystatus
        	''Stop MPLAB SIM Stopwatch
    	next i
        lcdout $FE,1,"NCD: ", idec mybyte
        pause 500
        
        return
        
    LOOKSUB:
    
        for i = 0 to 255
    	mystatus = i
    	''Start MPLAB SIM Stopwatch
        	lookdown2 mystatus, <= [%00000000, %00000001, %00000011, %00000111, %00001111, %00011111, %00111111, %01111111, %11111111], mybyte
        	''Stop MPLAB SIM Stopwatch
        next i
            
        lcdout,1, "Lookdown: ", idec mybyte
        pause 500
    
        return
    and here are the results.

    Name:  NCDTest.png
Views: 1035
Size:  7.7 KB
    Regards,
    TABSoft

  7. #7
    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?

    Lookdown2 makes 2x code over lookup, have you tested to confirm? Thanks
    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.

Similar Threads

  1. Replies: 6
    Last Post: - 18th July 2012, 03:42
  2. Whats the quickest way to set bits?
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 26th October 2010, 05:38
  3. Bit set question
    By nverma in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd April 2007, 22:23
  4. set flags within a variable?
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 24th October 2006, 12: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, 09: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