Shop OBEX P1 Docs P2 Docs Learn Events
Spin Syntax Questions — Parallax Forums

Spin Syntax Questions

ShawnaShawna Posts: 508
edited 2013-01-08 19:04 in Propeller 1
Hey Everyone,
Got a question for ya, is this the same as dividing by 2? y:= 200~>2
If so is this the same as dividing by 6? y:= 200~>6

If the above is true why not just do this? y:=200/2
Thanks
Shawn A

Comments

  • ratronicratronic Posts: 1,451
    edited 2013-01-03 11:12
    Shawn y := 200 ~> 2 is 200 divide by four and y := 200 ~> 6 is 200 divided by 64.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-01-03 11:15
    Shifting bits can be a short cut for dividing by powers of 2. The Propeller can shift bits much faster than it can divide.

    To divide by two, you only want to shift one bit.

    y:= 200~>1

    "y:= 200~>2"

    Will divide by 4.

    And "y:= 200~>6" divides by 32 not six.

    For positive numbers you can use shift ">>" instead of shift sign extend "~>".

    You need to use the "sign extend" version of sifting right or left when dealing with negative numbers because of the way the Propeller deals with negative numbers in binary (I think it's call "two compliment").

    The examples so far have been shifting bits right which acts as a divide, shifting bits left acts as a multiply.

    Window's built in calculator has a "Programmers" mode which lets you experiment with shift bits around converting between decimal, binary and hexadecimal.
  • SRLMSRLM Posts: 5,045
    edited 2013-01-03 11:18
    Not quite. The trick of using shift to divide is based on the principles of a binary counting system. If we apply the same technique to the decimal (base 10) counting system, we have something like:

    7545.610 / 1010 == 7545.610 -> 1 == 754.5610

    The subscript indicates the base. So, the shift operator effectively moves the decimal point. Notice that, with the shift in base 10, you can't divide (or multiply, if you shift left) by anything other than a power of 10.

    Now, on to binary and base 2. The number 20010 in binary is 110010002. If you shift it right by one, you get 11001002, which is 10010. If you shift 110010002 right by two, you get 5010, and so on.

    In general, if there is a choice, most embedded programmers will prefer to use a shift operation to multiply or divide by a power of two because a shift is much faster than it's equivalent multiply or divide.

    ps: my favorite binary,decimal,hex converter is here: http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html
  • ShawnaShawna Posts: 508
    edited 2013-01-03 11:24
    Thanks,
    I was reading threw one of Jason Dorie's programs and got stuck on a portion of his code. I still have problems with bit wise operations, and the syntax that goes with it.
    Shawn A
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-01-03 11:38
    SRLM wrote: »
    Not quite.

    SRLM,

    I assume you were clarifying some point I made but I'm still ignorate of anything I may wrote that was incorrect.

    If you're correcting something I wrote (which I always appreciate), I'd like to know what I stated incorrectly.


    Edit: Nevermind, I see you where addressing the OP. I just wanted to make sure I knew if I had said something incorrect.
  • ratronicratronic Posts: 1,451
    edited 2013-01-03 11:41
    I have read thru some of Jason's programming and he is one slick programmer. ~> is arithmetic bit shift right and also fills the shifted bits with the sign bit preserving the sign when you divide.
  • SRLMSRLM Posts: 5,045
    edited 2013-01-03 12:37
    Duane Degn wrote: »
    SRLM,
    ...
    Edit: Nevermind, I see you where addressing the OP. I just wanted to make sure I knew if I had said something incorrect.

    Yep... I was too slow to the draw, I guess :) When I posted both you and ratronic had beat me out :)
  • SRLMSRLM Posts: 5,045
    edited 2013-01-08 18:06
    Just for completeness:
    Wikipedia wrote:
    Logical right shifts are equivalent to division by a power of the radix (usually 2) only for positive or unsigned numbers. Arithmetic right shifts are equivalent to logical right shifts for positive signed numbers. Arithmetic right shifts for negative numbers in N-1's complement (usually two's complement) is roughly equivalent to division by a power of the radix (usually 2), where for odd numbers rounding downwards is applied (not towards 0 as usually expected). (Arithmetic right shifts for negative numbers would be equivalent to division using rounding towards 0 in one's complement representation of signed numbers as was used by some historic computers.)

    http://en.wikipedia.org/wiki/Arithmetic_shift

    So, be wary: arithmetic right shifting a negative number will round downwards, instead of towards 0.
  • ShawnaShawna Posts: 508
    edited 2013-01-08 19:04
    Thank You very much.
Sign In or Register to comment.