Shop OBEX P1 Docs P2 Docs Learn Events
Rotate Left/Right doesn't respect the length of the variable? — Parallax Forums

Rotate Left/Right doesn't respect the length of the variable?

photomankcphotomankc Posts: 943
edited 2012-04-03 16:06 in Propeller 1
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.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-03 14:36
    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)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-03 14:46
    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
  • photomankcphotomankc Posts: 943
    edited 2012-04-03 15:07
    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 DegnDuane Degn Posts: 10,588
    edited 2012-04-03 15:32
    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?
  • localrogerlocalroger Posts: 3,452
    edited 2012-04-03 16:06
    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)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-03 16:06
    Duane Degn wrote:
    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.
    Duane Degn wrote:
    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
Sign In or Register to comment.