Convert a long negative integer value to a word size value.
JohnR2010
Posts: 431
in Propeller 1
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.
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
16BitValue := LongValue
16BitValue |= LongValue >>16
If you check the manual there may be a 16bit signed shortcut that moves bit 31 to bit 15.
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
newval := oldval.word[0]
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.
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:
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!!
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.
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.
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!!
"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" 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);"> </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.