0

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?..

1
  • One question 4294967295 is not the max intger value in Javascript. Or you were just checking max 32 bit int value. Commented Nov 14, 2013 at 7:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What does ">>> 0" ? Shifting by 0 bits isn't the same thing again?
It is. But you have to use the >>> operator to get an unsigned result.
My approach does: 1111..1111 and than shift: 011111...111. What you do is 111111111111111 and shift nothing but delete sign bit: 0111...1111. What is the difference ? ? ?
I'm not deleting the sign bit. I'm treating 1111...1111 as unsigned.
Ok, this explains it. Thank you. +1 from me. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.