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


Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Nov 2008
    Posts
    96

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

    To continue my current PBP project it looks like I'll need to convert some 'signed' Words (a 16bit signed value read from consecutive bytes of a BMP085 barometric sensors calibration table).

    These 'signed' Words are treated as unsigned in PBP when I've read them from the sensor, but I use the S modifier to display them as -ve values on my LCD display.
    That was fine, but now I need to really use the true signed value in math operations, so I need to make the unsigned Word into a signed Long.

    So here's my hypothetical.
    If my Word var contains say the value -14293 in decimal (when Serout2 with SDEC is used to show it), how would I put that value in to a Long variable as signed hex and retain the proper sign of the value (that PBP Words don't care about) ?

    I know that I need to read the Words high bit and see if it's a 1 for a negative, then if so set the high bit of the Long to a 1, then copy the remaining bits into the right bits of the long (I think).

    I could do bit manipulation byte by byte I guess, but that sounds real messy. Is there a simpler way to have a 'signed' word and expand it into a Long ?

    Thanks,
    Martin

    PS. Actually I think I just thought of a way, but it there might be something better...

    Long = Word << 16 'Force the signed word up to the top 16 bits of a Long
    Long = Long >> 16 'Shift the Long value back 16 bits right, but PBP keeps the signed bit (high bit) set as required (would it?)
    ???
    Last edited by mr.sneezy; - 26th May 2010 at 12:58. Reason: Idea ?

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