Spin Syntax Questions
Shawna
Posts: 508
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
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
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.
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
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
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.
Yep... I was too slow to the draw, I guess When I posted both you and ratronic had beat me out
http://en.wikipedia.org/wiki/Arithmetic_shift
So, be wary: arithmetic right shifting a negative number will round downwards, instead of towards 0.