PDA

View Full Version : Rotate Left/Right doesn't respect the length of the variable?



photomankc
04-03-2012, 09:37 PM
So is it widely known that using <- and -> on values that are less than 32bits will rotate in the 0's from the upper, unused bits? If I define a word variable and give it the value of say:

10010110_11110000

Then if I rotate it right 8 bits I get:

00000000_10010110


So the zeros come marching in from memory that was not part of my variable. However if I define a long variable and use it then it works just as expected. I suppose this probably has to do with the hardware but I didn't see it mentioned in the manual beyond the fact that all the examples are 32bit. Spent a couple nights tearing my hair out before I finally honed on where things were going off the rails. Might be helpful if the manual mentioned that operator always assumes a 32bit value regardless of the size of the declared variable.

I know spin is not typed but the shift operators work exactly as I would expect for any given variable size. Wasn't expecting the rotate to be different.

Duane Degn
04-03-2012, 10:36 PM
Yeah, I've noticed this too.

I just make a point of only rotating bits with longs. I believe this is mentioned in somewhere in the manual or datasheet.

Phil Pilgrim (PhiPi)
04-03-2012, 10:46 PM
All math in Spin is long math. There's no such thing as a word or byte expression. The expression a ->= 2 means, "Load a into a 32-bit temporary register and zero-extend as necessary, rotate that register right by two, then store the lower 8, 16, or 32 bits of the result back into a, depending upon its size.

-Phil

photomankc
04-03-2012, 11:07 PM
Now that you mention it I remember that math was all 32 bit. Wasn't thinking of it as math. A little note thingy in the manual would have helped I think but that may have just been my ability to skim over important details sometimes.

Duane Degn
04-03-2012, 11:32 PM
All math in Spin is long math.

We can still add and subtract bytes, right?

I use longs if the math is at all complicated or if there might be a negative number, but it sure seems like a lot of math works on bytes and words.

Are there any kind of rules about when it's okay to do math operations on bytes or words and when one should only use longs?

localroger
04-04-2012, 12:06 AM
Adds and subtracts are long, but you don't notice because truncating them doesn't affect the result. You should assume that all operators and functions which work on numbers work on 32-bit longs and return 32-bit longs. By default, those are unsigned longs so you also get the trap of assigning -1 to a byte and getting 255 back when you read it; the ~ and ~~ prefix operators (as opposed to postfix, where they assign 0 and -1 values) sign-extend bytes and words so signed short negative values will work correctly.

Phil Pilgrim (PhiPi)
04-04-2012, 12:06 AM
We can still add and subtract bytes, right?
Sure, but they're converted to longs first, and 128 + 200 = 328, not 72. However, 328 gets converted to 72 when saved as a byte.


Are there any kind of rules about when it's okay to do math operations on bytes or words and when one should only use longs?
It's always okay, as long as you remember that the operations are taking place on 32-bit signed values before converting back to words or bytes by truncating off the high-order bits.

-Phil