-3

Since I noticed the last days that Javascript has a lot of individual operators designated by combinations of basic operators, I did a bit of research and found out there are a LOT(!) of good questions around (at least thats what the votes tell me).

I started yesterday and today by noticing those pretty unusual looking operators getting explained.

What does 'x << ~y' represent in JavaScript?

and

What does "!--" do in JavaScript?

So after I have seen this >>~ operator in one of the comments, I just was feeling the desire to ask this in the duty of our community.

What does the christmas-tree-operator actually do in a contect like this:

christmas=

!0
0 >>~
!-0;

Also I'm considering, would it be okay to let the candle burn, even if daddy wasn't observing:

christmas=

!0
0 >>~

And could we identify it christmas if the kids were gone, too:

christmas=

>>~

?

Community
  • 1
  • 1
dhein
  • 6,431
  • 4
  • 42
  • 74
  • 5
    Two operators: [Right shift](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#>>_(Sign-propagating_right_shift)) `>>` with [Bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT) `~` – Tushar Dec 18 '15 at 16:22
  • 4
    What is with the rant? – epascarello Dec 18 '15 at 16:24
  • 3
    I would consider reducing your question to the point where you just ask what this does. Otherwise many may not bother to read through all that. – Rob Dec 18 '15 at 16:25
  • 4
    Possible duplicate of [What does 'x << ~y' represent in JavaScript?](http://stackoverflow.com/questions/34349350/what-does-x-y-represent-in-javascript) – ElGavilan Dec 18 '15 at 16:26
  • 1
    @Tushar: while technically correct, you ignore the question about the candle and the childs. – dhein Dec 18 '15 at 16:28
  • 1
    @Zaibis If you know how those two operators work individually, it's easy to understand how they work together in any combination. – Tushar Dec 18 '15 at 16:30
  • 3
    I'm voting to close this question as off-topic because the question (is there one?) is a giant mess. – Andy Dec 18 '15 at 16:41
  • There could always be an edit to clean it up...? – TbWill4321 Dec 18 '15 at 16:42
  • @Andy: It is a well formed and simple question... – dhein Dec 18 '15 at 16:42
  • 2
    You're using SO as a platform to soliloquise. That's not what it's for. Anyway, you wanted us to downvote your question. Consider your question downvoted. – Andy Dec 18 '15 at 16:44
  • @Andy I hope you know how single-edged this sounds ;) – dhein Dec 18 '15 at 16:45
  • 1
    @Tushar: But tell me why you were not able to answer that about `!--` Its also just an not operation of an preincrementation. But thats worth 100 upvotes..... ridiciouless this was just an christmas joke (while it is still an absolutly on topic question btw....) But now I feel like I had to start a real rant... – dhein Dec 18 '15 at 19:29

1 Answers1

4

This line ends up getting a semicolon inserted after with ASI:

!0

Which is NOT 0 (a falsy value), which is true.

No ASI semicolon is inserted after the >> right shift and ~ bitwise NOT, so this is evaluated as one line:

0 >>~
!-0

Which can be (more correctly written) as:

0 >> ~!-0

Zero can only be right shifted to equal zero, but we'll break down the right side anyways. Bitwise NOT, Boolean NOT, and numeric cast of 0.

-0 == 0
!0 == true
~true = -2

Again, it doesn't matter what's on the right side of the right shift, since zero is just a bunch of zero bits.

TbWill4321
  • 8,626
  • 3
  • 27
  • 25