"Stretching" a byte variable into two byte variables?


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,670


    Did you find this post helpful? Yes | No

    Default Re: "Stretching" a byte variable into two byte variables?

    sample the msb of your byte
    if its set add three to your word var
    shift left the byte var 1 bit the word 2 bits
    repeat for all 8 bits
    Warning I'm not a teacher

  2. #2
    Join Date
    Feb 2013
    Posts
    1,150


    Did you find this post helpful? Yes | No

    Default Re: "Stretching" a byte variable into two byte variables?

    and if it not?

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,670


    Did you find this post helpful? Yes | No

    Default Re: "Stretching" a byte variable into two byte variables?

    don't add 3
    Warning I'm not a teacher

  4. #4
    Join Date
    Aug 2011
    Posts
    457


    Did you find this post helpful? Yes | No

    Default Re: "Stretching" a byte variable into two byte variables?

    If you start off with B=0 and C=0 you can skip half those statements and only do the ones where A.x = 1

  5. #5
    Join Date
    Feb 2013
    Posts
    1,150


    Did you find this post helpful? Yes | No

    Default Re: "Stretching" a byte variable into two byte variables?

    I wanted it to be shorter, by using loop and

    X.Y=Z
    but as I can see, you can't use another variable as bit reference, only static values.

  6. #6
    Join Date
    Feb 2013
    Posts
    1,150


    Did you find this post helpful? Yes | No

    Default Re: "Stretching" a byte variable into two byte variables?

    uint16_t enlargeYourByte(uint8_t input)

    2 {

    3 uint16_t x = input;

    4 x = (x ^ (x << 4)) & 0x0f0f;

    5 x = (x ^ (x << 2)) & 0x3333;

    6 x = (x ^ (x << 1)) & 0x5555;

    7 return x | (x << 1));

    8 }



    This is how they do it in C. Can we port this to PBP ?

  7. #7
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: "Stretching" a byte variable into two byte variables?

    I think this does it:
    Code:
    Input VAR BYTE
    Wide VAR WORD
    X VAR WORD
    Y VAR WORD
    
    EnlargeYourByte:
    X = Input
    Y = Input << 4
    X = X ^ Y
    X = X & $0F0F
    Y = X << 2
    X = X ^ Y
    X = X & $3333
    Y = X << 1
    X = X ^ Y
    X = X & $5555
    Wide = X << 1
    RETURN

Similar Threads

  1. "Variable already an alias" - is there a way to avoid this?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 18th June 2022, 22:22
  2. Byte from variable
    By Lilleljung in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th February 2015, 22:29
  3. Replies: 0
    Last Post: - 14th November 2013, 03:32
  4. Replies: 3
    Last Post: - 15th October 2012, 08:06
  5. AN Question for "word" variable read The serial port
    By redfoen in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th December 2007, 17:39

Members who have read this thread : 0

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