Rotate Left/Right doesn't respect the length of the variable?
photomankc
Posts: 943
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.
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.
Comments
I just make a point of only rotating bits with longs. I believe this is mentioned in somewhere in the manual or datasheet.
-Phil
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?
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