4


What does the notation somevar >> 0 mean in javascript?

Thanks

user2864740
  • 60,010
  • 15
  • 145
  • 220
albanx
  • 6,193
  • 9
  • 67
  • 97
  • 1
    `>>` is [bitshifting](http://www.javascripter.net/faq/arithmet.htm#shift) but I don't know why you would bitshift by 0 (unless there is some side effect that I am unaware of, it would do nothing...) – Reese Moore Dec 14 '10 at 08:25

3 Answers3

7

In a >> b, >> is a bitwise operator that shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off. Reference: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators

dheerosaur
  • 14,736
  • 6
  • 30
  • 31
3

Bitwise right shift. Although somevar >> 0 looks weird.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • It’s sort of confusing, though, since the only numeric type is the double. – Josh Lee Dec 14 '10 at 08:26
  • 3
    @Gumbo, becuase `somevar >> 0` is equal to `somevar` – dheerosaur Dec 14 '10 at 08:27
  • @dheerosaur Not necessarily: `34359739705 >> 0 == 1337`. – Josh Lee Dec 14 '10 at 08:28
  • @jleedev: how's that? I mean, how does a 0 bits shifting derives in a different value? – Tomas Narros Dec 14 '10 at 08:32
  • 2
    @jleedev, Oh, thanks. So, it converts the first operand to a 32 bit integer and shifts. Is that right? – dheerosaur Dec 14 '10 at 08:33
  • 2
    @Tomas Narros: The [Signed Right Shift Operator (`>>`)](http://bclary.com/2004/11/07/#a-11.7.2) is only specified for signed 32 bit integers. So `34359739705 & 0x8FFFFFFF` is performed to get a signed 32 bit integer. And `34359739705 & 0x8FFFFFFF === 1337`. – Gumbo Dec 14 '10 at 08:38
1

It's a bitwise operator. In this case, for shifting the first operand in binary representation the number of bits to the right specified in the second operand, discarding bits shifted off.

With a 0 as second operand, I guess it has no effect (shifting 0 bits, is getting the same value?).

I was wrong with this last. As explained at this @Gumbo's comment.

Community
  • 1
  • 1
Tomas Narros
  • 13,390
  • 2
  • 40
  • 56