Reputation: 6206
Sorry if this is a duplicate question; was unable to find an answer anywhere.
Does the Javascript standard guarantee a deterministic result for ~0
?
Chrome gives me -1
for <script>alert(~0);</script>
.
Is this result guaranteed cross-browser (and cross-platform in general)?
Upvotes: 1
Views: 112
Reputation: 239643
- Let expr be the result of evaluating UnaryExpression.
- Let oldValue be ToInt32(GetValue(expr)).
- Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.
The first step actually evaluates the expression. Second step converts the expression to a 32 bit integer. All implementations should implement this properly. The third step is simply to flip the bits in the number. There is only one way to do this.
So I would expect all the implementations to give the same result.
Upvotes: 2
Reputation: 782148
Yes, it's deterministic. From the MDN documentation of bitwise NOT
Bitwise NOTing any number
x
yields-(x + 1)
.
This is because the bitwise operators are based on two's complement notation.
Upvotes: 2