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

  • Home
  • SEARCH
  • 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 8211627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:29:27+00:00 2026-06-07T10:29:27+00:00

sorry for such a specific question but upon looking at the following algorithm written

  • 0

sorry for such a specific question but upon looking at the following algorithm written in Javascript

  function c(a) {
    if (a < 2) return 2;
    if (a > 4096) return 4096;
    var b = a & (a - 1);
    while (b > 0) {
        a++;
        b = a & (a - 1)
    }
    return a
}

I came accross a statement I wasn’t sure about. What exactly does var b = a & (a - 1); actually do? I was under the assumption it assigned A to B and then subtracted 1 from B, however, if that was the case then wouldn’t B never reach 0 (or below 0) resulting in an infinite loop? How can this work?

I ask this because I have attempted to adapt the algorithm to PHP but have hit a wall. It works flawlessly in Javascript, so I know for certain that I’m not understanding what is happening. Here is my attempt in PHP:

function c($a) {
    if ($a < 2) return 2;
    if ($a > 4096) return 4096;
        $b = $a 
        $b = ($b - 1);
    while ($b > 0) {
        $a++;
        $b = $a;
        $b -= 1;   
    }
    return $b;
}

I can see clearly why it doesn’t work but I’m not sure how to change the algorithm to make it work. More or less, I know that I am not adapting the algorithm properly because I don’t understand how it works in Javascript.

Either way, please help me! I don’t specifically want someone to work out my problem for me but a hint in the right direction would be really great. 🙁

Thanks alot.

  • 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-07T10:29:29+00:00Added an answer on June 7, 2026 at 10:29 am

    That line clears the lowest set bit in the value of a and assigns the result to b.

    Example:

    00010100110101111000
    

    Becomes :

    00010100110101110000
                    ^
    

    The reason it works is that subtracting one flips all the bits up to and including the least significant bit that was set. All other bits remain unchanged. Using bitwise-and keeps all the bits that have not changed.

    00010100110101111000  a
    00010100110101110111  a-1
    00010100110101110000  a & (a-1)
    

    This loop repeatedly adds one to a until clearing one bit of a gives zero:

    b = a & (a - 1);
    while (b > 0) {
        a++;
        b = a & (a - 1);
    }
    

    In other words, it rounds a up to the nearest power of 2 in a very inefficient way!

    Related

    • Bit Twiddling Hacks – Round up to the next highest power of 2
    • What are bitwise shift (bit-shift) operators and how do they work?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry to ask such a simple question, but it's a specific question I've
Sorry for such a trivial question, but I'd feel silly building this if it
Sorry for such a silly question but Python docs are confusing... Link 1: Queue
Sorry to ask such a noob question, but the NDK documentation is wrong (r7b):
sorry this is such a simple question but I can't figure it out. How
I am very sorry to ask such a basic question but I am new
Sorry for such a basic question, but I need to know how to add
sorry, i dont like to ask such instance specific questions but this is driving
Sorry for such a vague question, but I have been searching around for the
Sorry for such general question, but what is the best (as fast as possible

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.