On Sat, Feb 9, 2013 at 2:32 AM, Leo Lunatic <[email protected]> wrote:

> Given:
>   a=0x80000000
>   b=0x80000000>>0
>   c=0x80000000>>>0
> we find
>   a !== b
> but
>   a === c
> and this is LOVELY!
>   a !== ~~a
>
> I realized this is probably and artifact of the optimizations we know and
> love,
>

It's an artifact of the JavaScript language specification, which we all
know and, uh, ...

All numbers in JavaScript are doubles, and generally signed. The bitwise
operators, however, have "truncation to int32" semantics. So in your
example, "~a" converts the double 2147483648.0 (which you got by typing
0x80000000) to integer, resulting in the bit pattern 0x80000000, then flips
all bits. The result is the bit pattern 0x7FFFFFFF. When evaluating "~~a",
the same is now performed again, and the result is the *bit pattern*
0x80000000, which when interpreted as a signed int32 and converted back to
double is -2147483648.0. Now when you compare 2147483648.0
and -2147483648.0, of course they're not the same!
The same explanation holds for "a >> 0", which has signed semantics. "a >>>
0" behaves differently, because it has unsigned semantics, i.e. it
interprets both its input and its output as unsigned 32-bit integers. (If
">>" and ">>>" did the same thing, then what would be the point in having
both?)

but when commutativity and identity are messed with it's feels like cats and
> dog living together; you know, end of the world stuff :).
>

Nothing is messed with. The JavaScript bitwise operators perform an
implicit conversion, and then a bitwise operation. Even if the latter is
involutary, applying the operator twice does not undo the former. So the
operator as a whole is an involution if the conversion is a no-op, which
means "a === ~~a" is true if and only if "a" contains a valid signed int32
value.

Bit patterns are hard. So are dynamically typed languages. Let's go
shopping.


>
> Be warned if you aren't already.
>
> G'day,
> -LLeo
>
> P.S. If you have trouble visualizing the bits printf("0b%032b", ...) is
> your
> friend. https://github.com/wdavidw/node-printf (works in browsers).
>
>  --
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to