Shop OBEX P1 Docs P2 Docs Learn Events
Convert a long negative integer value to a word size value. — Parallax Forums

Convert a long negative integer value to a word size value.

I have done this in the past but I cant remember the process.  To test the new forum I thought I would pose the question.

I have a value -23 stored in a long (32 bit)  I need to convert it to a Signed 16-bit integer value.  How can I do that in SPIN?

Just trying to get everyone's mind off the form change.  

Comments

  • T ChapT Chap Posts: 4,223
    edited 2015-06-30 15:48
    Bitshift bit 31 to the right 16  then make your word = to the result in two stages. 

    16BitValue := LongValue
    16BitValue |= LongValue >>16 

    If you check the manual there may be a 16bit signed shortcut that moves bit 31 to bit 15. 



  • Dave HeinDave Hein Posts: 6,347
    edited 2015-06-30 16:31
    Spin doesn't have a signed 16-bit value, but you use the "~~" operator to do sign extension on a 16-bit value.  Here's an example:
    VAR
      long x, y;
      word z;


    PUB example
      x := -2
      z := x ' Lower 16 bits of x stored in z
      y := ~~z ' Convert 16-bit value in z to a 32-bit signed value




  • Simply copy to a 16-bit variable. If you want to make the code very obvious, you can do something like this:
    newval := oldval.word[0]
  • Heater.Heater. Posts: 21,230
    edited 2015-06-30 16:34
    -23 in a binary 32 bit number will have it's MSB set to one and many of the bits under that. 
    For example -1 is 11111111111111111111111111111111 in 32 bit two's complement.-23 is 11111111111111111111111111101001

    So
    16BitValue := LongValue
    is all you need to make a 32 bit signed value into a 16 bit signed value.

  • Heater.Heater. Posts: 21,230
    Sorry to mention the broken forum but undoing that bold text that I did not order is just too hard what with the kilobytes of HTML junk that is around it.
  • To add to what Dave said, there is really nothing to done, it is as simple as,
    wordvalue := longvalue
    So long as the value is in the range -32768 to +32767. Then to reconstruct it as a long use prefix operator ~~. :cool:
  • JohnR2010JohnR2010 Posts: 431
    edited 2015-06-30 17:50
    Turns out the problem I'm having is not with the SPIN code or the propeller its the device I'm sending to.

    As several of you pointed out I really didn't need to do anything as the propeller stores -23 as:
    Dec -23
    As a Long 0xFFFF FFE9
    As a Word 0xFFE9
    What got me was when I was troubleshooting the problem I printed the  word sized value as a long to the screen with the pst.dec(val) and it printed 65,517 not what I expected and that sent me off down this road.  It didn't take me long to catch myself and figure out all is okay on the propeller side its the JAVA code on the receiving side I need to focus on.

    I can say its so good to see all you guys chime in and help. This is the all
    star list of brain power minus a few others that must not be on-line
    right now!!  I think this community is so strong and I know for me
    you guys are people I think about often so I hope we all make it
    through this forum change intact!! 

    Hang in their @Heater its going to get better!!  I know it will!!
  • Heater.Heater. Posts: 21,230
    JohnR2010
    I have no plans to go anywhere. Despite my loud complaining. 
    Not as long as there are curious people with questions I can perhaps answer. As long as there are smarter and more knowledgeable people around who can answer my questions. 

     
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-06-30 18:36
    Sorry to mention the broken forum but undoing that bold text that I did not order is just too hard what with the kilobytes of HTML junk that is around it.

    Undoing bold text is easy.  You just need to go into the edit mode, select the bolded text and then hit the bold button on the edit taskbar.  Of course, you only have a 60 minute time limit to complete the task.
  • JohnR2010JohnR2010 Posts: 431
    edited 2015-06-30 19:16
    Changed the server's JAVA code (SmartThings device type) and fixed my problem.

    temp = a long value

    From:
    def celsius = Integer.parseInt(temp, 16)

    To:
    def celsius = Integer.valueOf(temp,16).shortValue()

    Before the change, temperatures where showing up just fine as long as they were above 0.  When they went negative the two's complement word size value was being interpreted incorrectly by the Integer.ParseInt command.  Turns out the problem had noting to do with my SPIN code.  Found the answer on StackOverflow as it was a JAVA problem.  Thanks again everyone!!
  • Heater.Heater. Posts: 21,230
    edited 2015-06-30 19:32
    Dave Hein 

    "Undoing bold text is easy"

    No doubt true, but undoing formatting that includes bold, size and colour, etc is not so easy. 
    One might expect the "remove formatting button to do it but it does not. 
    For example the above "Dave Hein"  is actually:<pre><div><a href="http://forums.parallax.com/profile/46446/Dave Hein&quot; class="Username" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: bold; font-size: 15px; font-family: Calibri, Arial, Helvetica, Tahoma, Segoe, 'Segoe UI', Verdana, sans-serif; vertical-align: baseline; text-decoration: none; color: rgb(29, 86, 137); line-height: 22.1000003814697px; background-color: rgb(255, 255, 255);">Dave Hein</a><span style="font-family: Calibri, Arial, Helvetica, Tahoma, Segoe, 'Segoe UI', Verdana, sans-serif; font-size: 13px; line-height: 22.1000003814697px; background-color: rgb(255, 255, 255);">&nbsp;</span><br></div><div style="font-style: normal; font-variant: normal; font-weight: normal;"><span style="font-family: Calibri, Arial, Helvetica, Tahoma, Segoe, 'Segoe UI', Verdana, sans-serif; font-size: 13px; line-height: 18.2000007629395px; background-color: rgb(255, 255, 255);"><br></span></div><div style="font-style: normal; font-variant: normal; font-weight: normal;"><span style="font-family: Calibri, Arial, Helvetica, Tahoma, Segoe, 'Segoe UI', Verdana, sans-serif; font-size: 13px; line-height: 18.2000007629395px; background-color: rgb(255, 255, 255);">
    <pre>The edit box cannot handle that. 
Sign In or Register to comment.