Shop OBEX P1 Docs P2 Docs Learn Events
Help combining MSB & LSB — Parallax Forums

Help combining MSB & LSB

yousefyousef Posts: 21
edited 2011-08-13 08:07 in Propeller 1
I'm reading the outputs of an accelerometer as a MSB (8 bits) and LSB (8 bits). i'm new to spin programming and i need to combine the two numbers into one. the two numbers are given in two's complement.

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-08-13 06:50
    It may well be as simple as this
    PRI combine_unsigned(MSB, LSB)
    
      result.byte[0] := LSB
      result.byte[1] := MSB
    
    PRI combine_signed(MSB, LSB)
    
      result.byte[0] := LSB
      result.byte[1] := MSB
    
      return ~~result                  ' sign-extend word to long
    
  • yousefyousef Posts: 21
    edited 2011-08-13 07:08
    Thanks, that helped :)
  • yousefyousef Posts: 21
    edited 2011-08-13 07:18
    How to remove the last two bits(LSB) to make it 14 bits number?
  • LeonLeon Posts: 7,620
    edited 2011-08-13 07:37
    Right shift?
  • yousefyousef Posts: 21
    edited 2011-08-13 07:45
    yep, for example want 1001111111111101 to be 10011111111111
  • LeonLeon Posts: 7,620
    edited 2011-08-13 07:52
    Don't you want the result to be 0010011111111111? It'll still be a 16-bit value.
  • yousefyousef Posts: 21
    edited 2011-08-13 07:57
    its ok as long as i can get rid of the last two bits
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-08-13 07:59
    yousef wrote: »
    How to remove the last two bits(LSB) to make it 14 bits number?

    If the value is signed:
    result := result ~> 2
    

    This removes two LSB bits from the value while preserving the sign.
  • yousefyousef Posts: 21
    edited 2011-08-13 08:07
    yeah it worked, thanks
Sign In or Register to comment.