Number Sort Algorithm


Results 1 to 40 of 55

Threaded View

  1. #11
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    How this logic works is pretty cool. I saw this on the PIClist a few years back.

    Here's how it works.

    a VAR BYTE
    b VAR BYTE
    a = %11001100
    b = %00110011

    a=a^b ' a now = %11001100 ^ %00110011 which = %11111111

    1 ^ 0 = 1. 1 ^ 1 = 0. 0 ^ 0 = 0.

    b=a^b ' b now = %11111111 ^ %00110011 which = %11001100 (value of original a)

    b now contains the original value of a.

    a=a^b ' a now = $11111111 ^ %11001100 which = %00110011 (value of orignal b)

    Now that b = the original value held in a, ^-oring a with b returns the orignal
    value of b, in a.

    Using any two values, it still works the same. Like this;

    a = %11011100
    b = %00110011

    a=a^b ' a now = %11011100 ^ %00110011 which = %11101111
    b=a^b ' b now = %11101111 ^ %00110011 which = %11011100 (value of original a)
    a=a^b ' a now = $11101111 ^ %11011100 which = %00110011 (value of orignal b)

    Pretty nifty way of swapping variables.
    Last edited by Bruce; - 25th July 2007 at 21:16.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Dynamic USB Serial Number (PIC18F4550)
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th July 2009, 17:03
  2. Working with the random number generator
    By kwelna in forum mel PIC BASIC
    Replies: 9
    Last Post: - 16th January 2007, 17:50
  3. Random number results
    By bartman in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th March 2005, 17:39
  4. more random number questions
    By bartman in forum mel PIC BASIC
    Replies: 1
    Last Post: - 14th November 2004, 17:55
  5. Split number into variables
    By psmeets in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th January 2004, 04:15

Members who have read this thread : 3

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