Shop OBEX P1 Docs P2 Docs Learn Events
Trying to understand operators. — Parallax Forums

Trying to understand operators.

Hi again Smart People,

SPIN is kicking my butt a bit. My past experience is ZX Spectrum BASIC. In simple English would someone please decipher this so I may wrap my head around how I would have done it in BASIC?

x_value = x1+(x1->2)+(x1->4)

Any help would be appreciated.

Thank you.

Comments

  • The "->" operation is a 32-bit rotate right. There's no equivalent in Basic. "x1->2" takes the low order 2 bits of x1 and rotates them into bit positions 31 and 30 shifting the rest of x1 to the right effectively dividing it by 4. In other words, aaaa bbbb cccc dddd eeee ffff gggg hhhh becomes hhaa aabb bbcc ccdd ddee eeff ffgg gghh. Similarly, "x1->4" takes the low order 4 bits of x1 and rotates them into bit positions 31-28 shifting the rest of x1 to the right effectively dividing it by 16. The rest of the statement just adds the 3 terms together.
  • Isn't a divide a shift right, not a rotate right? I cannot think of why you would want to add rotated values.

    John Abshier
  • Yes, divide by a power of 2 is effectively a shift right. The "->" rotate right isn't quite the same thing. A "~>" using a tilde as the first character is an arithmetic shift right which does act like a divide by a power of 2. Maybe that's what you're actually using.
  • Holy cow! Buckle up because this is going to sound stupid. It is a Tilde. In the Propeller tool on my new 14" monitor the tilde looks like a minus. Now I understand.

    KB
    800 x 277 - 108K
  • JonnyMacJonnyMac Posts: 8,912
    edited 2019-02-05 15:43
    ~> is the Spin equivalent of SAR (signed arithmetic shift right); it's useful for dividing signed numbers by powers of 2.
  • CalvinBreen,

    It looks like a tilde (~) which I've seen mostly used to sets I/O pins to High and Low values.

    All of the Spin operators are listed on Page 144 of the Propeller Manual.
    https://www.parallax.com/sites/default/files/downloads/P8X32A-Web-PropellerManual-v1.2.pdf

    You can see from the table that there are 3 tilde operators which are explained in detail on Pages 156 to 159.
    ~ ' Sign-extend from bit 7 (~X) or post-clear to 0 (X~); all bits low, 156
    ~~ ' Sign-extend from bit 15 (~~X) or post-set to -1 (X~~); all bits high, 157
    ~> ' Bitwise: Shift arithmetic right, 158
    

  • Excellent, got it. Thank you. Man I love this forum.
  • Excellent, got it. Thank you. Man I love this forum.

    yeah, be careful, it is addictive...

    Enjoy!

    Mike
  • kwinnkwinn Posts: 8,697
    msrobots wrote: »
    Excellent, got it. Thank you. Man I love this forum.

    yeah, be careful, it is addictive...

    Enjoy!

    Mike

    So true, but it's a healthy addiction.
Sign In or Register to comment.