what does >> do in actionscript 3.0?
An example of it being used is here
boundsCentre = new Vector3D(sta开发者_JAVA技巧geWidth >> 1,stageHeight >> 1, 200.0);
Thanks.
>>
is the right bit-shifting operator. a >> b
will shift a
's bit values b
bits to the right.
if a = 8
and b = 2
, a >> b
will produce 2
because a
is represented by 1000
, shifted 2 places is 10
which represents 2
in binary.
More importantly, because ActionScript is an ECMAScript variant, values of type Number
will be converted from a 64-bit representation to a 32-bit representation and then shifted. Additionally, (AFAIK, can't seem to find it in the reference) bit-shift overflows are undefined in ECMAScript.
stageWidth >> 1
essentially divides the stageWidth by 2, which means it's a vector to the center of the stage. In other languages x >> 1
is a faster method of division by 2, but in ECMAScript, there is no significant performance change, and the possibility of ambiguity. Because of this it is better to just code the desired effect as:
stageWidth / 2
It is much more understandable that way.
It's a bitwise right shift. See this page
A bitwise right shift is, essentially, integer division by 2 for each position you shift. x >> 2 divides by 4, x >> 3 divides by 8, etc. Conversely, << is a left shift which is integer multiplication by 2.
Consider this example, the number 42 is, in binary
00101010
Right shifting by one position results in this:
00010101
Which is 21.
精彩评论