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 8436589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:13:06+00:00 2026-06-10T07:13:06+00:00

I recently found this piece of JavaScript code: Math.random() * 0x1000000 << 0 I

  • 0

I recently found this piece of JavaScript code:

Math.random() * 0x1000000 << 0

I understood that the first part was just generating a random number between 0 and 0x1000000 (== 16777216).

But the second part seemed odd. What’s the point of performing a bit-shift by 0? I didn’t think that it would do anything. Upon further investigation, however, I noticed that the shift by 0 seemed to truncate the decimal part of the number. Furthermore, it didn’t matter if it was a right shift, or a left shift, or even an unsigned right shift.

> 10.12345 << 0
10
> 10.12345 >> 0
10
> 10.12345 >>> 0
10

I tested both with Firefox and Chrome, and the behavior is the same. So, what is the reason for this observation? And is it just a nuance of JavaScript, or does it occur in other languages as well? I thought I understood bit-shifting, but this has me puzzled.

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-06-10T07:13:08+00:00Added an answer on June 10, 2026 at 7:13 am

    You’re correct; it is used to truncate the value.

    The reason >> works is because it operates only on 32-bit integers, so the value is truncated. (It’s also commonly used in cases like these instead of Math.floor because bitwise operators have a low operator precedence, so you can avoid a mess of parentheses.)

    And since it operates only on 32-bit integers, it’s also equivalent to a mask with 0xffffffff after rounding. So:

    0x110000000      // 4563402752
    0x110000000 >> 0 // 268435456
    0x010000000      // 268435456
    

    But that’s not part of the intended behaviour since Math.random() will return a value between 0 and 1.

    Also, it does the same thing as | 0, which is more common.

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

Sidebar

Related Questions

I just recently found myself writing this line of code, which i did not
Recently I found a piece of code that creates an instance of TButton from
I recently found this little piece of code for my .vimrc if has(autocmd) Highlight
Recently I was working with some JavaScript code and I found this: <script type=text/javascript>
I recently found this post . It basically says that Eclipse has a modified
I've recently found this blog entry on a tool that writes XSS attacks directly
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
EDIT 9-3-10: I found this blog entry recently that was very enlightening. http://optimizermagic.blogspot.com/2007/12/outerjoins-in-oracle.html There
This question is based off some really odd code I recently found in a
I recently found this web page Crime in Downtown Houston that I'm interested in

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.