Liglo App
Liglo App

Reputation: 3819

Finding JS max integer value in a funny way fails

Today I tried to find a funny and mysterious way to determine JavaScript's maximal integer value. One of the approaches was the following:

~(+!!![]) >>> (+!![]);

which evaluates actually to

~0 >>> 1

but it returns 2147483647 and not 4294967295 as it should. Why? Of course, the latter one would be the result of this operation for an unsigned integer, while my result is correct for a signed one. But how to force it?..

Upvotes: 0

Views: 53

Answers (1)

Barmar
Barmar

Reputation: 781716

You're finding the maximum integer, and then shifting it to the right 1 bit, which divides it by 2. Use:

~0 >>> 0

to get the maximum integer.

Converting that to the "funny" way I'll leave as an exercise for the reader.

Upvotes: 2

Related Questions