views:

663

answers:

3

I came across this in some AS 3.0 code:

(duration >> 0)

Where duration is a Number. I think I know what a bitwise right shift does, but what's the point of shifting 0 bits? This happens a few times in the code, I'd like to understand what it does.

+2  A: 

Sometimes you'll see this in an enum just for completeness, i.e.

flag0 = 1 >> 0,
flag1 = 1 >> 1,
flag2 = 1 >> 2

and so on. If you encounter this in code, I would expect that it is just some leftover from when there was actually more to shift (testing, changed requirements). That is, of course, unless this operator is not a shift operator in AS3. In C++, for example, it is also used as input stream operator (you know, because you're shifting some input into some variable. Sigh).

OregonGhost
>> is definitely a bitshift operator in AS3
Richard Szalay
+2  A: 

It might be a trick to get rid of the decimals without the overhead of a call to Math.floor() since even with a Number as input you will get an integer back.

I'm not sure how this compares performance wise to doing a (more commonly seen) cast to int(x)

grapefrukt
+2  A: 

Number to integer conversion Using int(x) is 10% faster in AS3. Still the bitwise version works better in AS2.

x = int(1.232)

//equals: x = 1.232 >> 0;

best explained:http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/das