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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:07:44+00:00 2026-05-25T13:07:44+00:00

Line 294 of java.util.Random source says if ((n & -n) == n) // i.e.,

  • 0

Line 294 of java.util.Random source says

if ((n & -n) == n) // i.e., n is a power of 2
    // rest of the code

Why is this?

  • 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-05-25T13:07:45+00:00Added an answer on May 25, 2026 at 1:07 pm

    The description is not entirely accurate because (0 & -0) == 0 but 0 is not a power of two. A better way to say it is

    ((n & -n) == n) when n is a power of two, or the negative of a power of two, or zero.

    If n is a power of two, then n in binary is a single 1 followed by zeros.
    -n in two’s complement is the inverse + 1 so the bits lines up thus

     n      0000100...000
    -n      1111100...000
     n & -n 0000100...000
    

    To see why this work, consider two’s complement as inverse + 1, -n == ~n + 1

    n          0000100...000
    inverse n  1111011...111
                         + 1
    two's comp 1111100...000
    

    since you carry the one all the way through when adding one to get the two’s complement.

    If n were anything other than a power of two† then the result would be missing a bit because the two’s complement would not have the highest bit set due to that carry.

    † – or zero or a negative of a power of two … as explained at the top.

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

Sidebar

Related Questions

This line in YUI's Reset CSS is causing trouble for me: address,caption,cite,code,dfn,em,strong,th,var { font-style:
This line of code ((Matches)Container.DataItem).MatchID works in C# but in VB.NET, when used in
this line of the code makes the form see-through User32Wrappers.SetLayeredWindowAttributes(Me.Handle, 0, _ 255 *
This line of code is working fine for Firefox $(#<%=txt1.ClientID%>).text() but not for IE8
This line of code is in AController.m UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
This line of code works on my computer (64-bit Win7). I tested on XP
This line of code does not produce the result I am expecting. NSString *tmpstoryTitle2
(line of code of interest is the last one, the rest is just for
This line of code DbProviderFactory factory = DbProviderFactories.GetFactory(Microsoft.SqlServerCe.Client); throws this exception System.Configuration.ConfigurationErrorsException: Failed to
This line of code, which decodes an encoded Chinese word: URLDecoder.decode(%E4%BB%BB%E4%BD%95%E8%BD%A6%E8%BE%86%E5%BA%94, UTF-8).getBytes().length When I

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.