Convert a 'signed' Word to a Long with minimum manipulation ?


Results 1 to 8 of 8

Threaded View

  1. #5
    Join Date
    Nov 2008
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    Yes. The method above works well. I did not try the other way to see if it's tighter code though, I was happy to just push onwards.

    I discovered a issues with the rest of my calculations though, and I'd appreciate a comment from the 'Long literates' among us.

    I'll try to explain without a huge dialogue, if it's too vague let me know. Here goes...

    OK, this code, all the variables are Longs, except AC5, AC6 and MD which are Words.

    Code:
    X1 = ((lUtemp - AC6) * AC5) >>15  'find X1 - Note I needed the extra () around AC5 to get it to work
             X2 = (lMC << 11) / (X1 + MD)      'Find X2
            B5 =  X1 + X2                               'Find B5 from X1 and X2
            lCTemp = (B5 + 8) / 16    '>> 4      'Hey presto, lCTemp appears...
    Firstly I needed two sets of parenthesis around the X1 calculation, or it gave a bad result with only one around the 'lUTemp -AC6'.
    The original C code was like this though ( I changed the 2^15 to the shift right by 15).
    X1 = (UT - AC6) * AC5 / 2^15

    Second, in the last calculation, I got a bad result whenever the value B5 became negative, until I got rid of the >> 4 and made that a '/ 16'. Then the value of lCTemp worked OK below 0 degC (I used freezer spray). I thought a >> 4 shift was equivalent to a / 16, but here in this syntax it seems not.

    Can someone fill me in on why the shifts didn't quite work like a numeric divide here ?
    Perhaps something to do with signed math again ?

    In the end with the second brackets and a numeric divide the results were good, I'd just like to know why my 'fixes' worked so I don't bugger up the next stage...

    Thanks,
    Martin
    Last edited by mr.sneezy; - 27th May 2010 at 12:35.

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