Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 50851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:39:29+00:00 2026-05-10T16:39:29+00:00

I heard that you could right-shift a number by .5 instead of using Math.floor().

  • 0

I heard that you could right-shift a number by .5 instead of using Math.floor(). I decided to check its limits to make sure that it was a suitable replacement, so I checked the following values and got the following results in Google Chrome:

 2.5 >> .5 == 2; 2.9999 >> .5 == 2; 2.999999999999999 >> .5 == 2;  // 15 9s 2.9999999999999999 >> .5 == 3;  // 16 9s 

After some fiddling, I found out that the highest possible value of two which, when right-shifted by .5, would yield 2 is 2.9999999999999997779553950749686919152736663818359374999999¯ (with the 9 repeating) in Chrome and Firefox. The number is 2.9999999999999997779¯ in IE.

My question is: what is the significance of the number .0000000000000007779553950749686919152736663818359374? It’s a very strange number and it really piqued my curiosity.

I’ve been trying to find an answer or at least some kind of pattern, but I think my problem lies in the fact that I really don’t understand the bitwise operation. I understand the idea in principle, but shifting a bit sequence by .5 doesn’t make any sense at all to me. Any help is appreciated.

For the record, the weird digit sequence changes with 2^x. The highest possible values of the following numbers that still truncate properly:

 for 0: 0.9999999999999999444888487687421729788184165954589843749¯ for 1: 1.9999999999999999888977697537484345957636833190917968749¯ for 2-3: x+.99999999999999977795539507496869191527366638183593749¯ for 4-7: x+.9999999999999995559107901499373838305473327636718749¯ for 8-15: x+.999999999999999111821580299874767661094665527343749¯ ...and so forth 
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:39:30+00:00Added an answer on May 10, 2026 at 4:39 pm

    Actually, you’re simply ending up doing a floor() on the first operand, without any floating point operations going on. Since the left shift and right shift bitwise operations only make sense with integer operands, the JavaScript engine is converting the two operands to integers first:

    2.999999 >> 0.5 

    Becomes:

    Math.floor(2.999999) >> Math.floor(0.5) 

    Which in turn is:

    2 >> 0 

    Shifting by 0 bits means ‘don’t do a shift’ and therefore you end up with the first operand, simply truncated to an integer.

    The SpiderMonkey source code has:

    switch (op) {   case JSOP_LSH:   case JSOP_RSH:     if (!js_DoubleToECMAInt32(cx, d, &i)) // Same as Math.floor()         return JS_FALSE;     if (!js_DoubleToECMAInt32(cx, d2, &j)) // Same as Math.floor()         return JS_FALSE;     j &= 31;     d = (op == JSOP_LSH) ? i << j : i >> j;     break; 

    Your seeing a ’rounding up’ with certain numbers is due to the fact the JavaScript engine can’t handle decimal digits beyond a certain precision and therefore your number ends up getting rounded up to the next integer. Try this in your browser:

    alert(2.999999999999999); 

    You’ll get 2.999999999999999. Now try adding one more 9:

    alert(2.9999999999999999); 

    You’ll get a 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I heard that it's possible to identify the brand of a device (or at
I heard that having @foreach inside of a view is a no-no. Meaning, the
I heard that Google can track all the static pages in a website and
I heard that you need to create images for both retina and non-retina. And
I heard that SQL is faster if table relationships are defined. Is this true?
I heard that JNBridge will translate C# code to native Java code.( I haven't
I heard that C# is a rapid application development (RAD) language. Even after reading
I heard that in C, if I do char *s = hello world. the
I heard that in PHP there are some alternatives for the functions substr() and
I've heard that it is a bad idea to do something like this. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.