Shop OBEX P1 Docs P2 Docs Learn Events
Show me a SIGN — Parallax Forums

Show me a SIGN

ErlendErlend Posts: 612
edited 2020-02-09 10:32 in Propeller 1
In my application I am reading an integer value from a chip register - in a 24bit/3Byte format. When the value is negative the topmost bit is one (as it should be). I am reading the bits into a LONG and need to make sure the sign is recovered in this (4Byte) format. I believe the ~ (sign extend) operator will only do this when you want to go from BYTE to LONG? Here is how I intend to do this:
PRI INT24to32(ptrValue) | value
    
    value:= LONG[ptrValue]                                                     'read from ptrValue which is a LONG where b24-31 are unused 0                                                  '
    IF value >> 23 & 1 = 1                                                      'check if bit23 is set - means negative number - if negative
      value:= value | $FF_00_00_00                                             'extend the sign all the way up to b31
   
    LONG[ptrValue]:= value                                                     'write back to ptrValue
Is there a better way?

Erlend

Comments

  • Wuerfel_21Wuerfel_21 Posts: 4,448
    edited 2020-02-09 10:23
    How about just this:
    PRI INT24to32(ptrValue)
        LONG[ptrValue]:= (LONG[ptrValue] << 8) ~> 8 ' 24 bit sign extension
    
  • Excellent! I hadn't discovered the ~> operator - Bitwise Shift Arithmetic Right. Thank you :)
    Erlend
Sign In or Register to comment.