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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:52:02+00:00 2026-05-12T09:52:02+00:00

If I have a integer number n , how can I find the next

  • 0

If I have a integer number n, how can I find the next number k > n such that k = 2^i, with some i element of N by bitwise shifting or logic.

Example: If I have n = 123, how can I find k = 128, which is a power of two, and not 124 which is only divisible by two. This should be simple, but it eludes me.


Related questions for some specific languages:

  • C: Rounding up to next power of 2
  • C++: Algorithm for finding the smallest power of two that's greater or equal to a given value (including C++20 std::bit_ceil())
  • 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-12T09:52:02+00:00Added an answer on May 12, 2026 at 9:52 am

    For 32-bit integers, this is a simple and straightforward route:

    unsigned int n;
    
    n--;
    n |= n >> 1;   // Divide by 2^k for consecutive doublings of k up to 32,
    n |= n >> 2;   // and then or the results.
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    n++;           // The result is a number of 1 bits equal to the number
                   // of bits in the original number, plus 1. That's the
                   // next highest power of 2.
    

    Here’s a more concrete example. Let’s take the number 221, which is 11011101 in binary:

    n--;           // 1101 1101 --> 1101 1100
    n |= n >> 1;   // 1101 1100 | 0110 1110 = 1111 1110
    n |= n >> 2;   // 1111 1110 | 0011 1111 = 1111 1111
    n |= n >> 4;   // ...
    n |= n >> 8;
    n |= n >> 16;  // 1111 1111 | 1111 1111 = 1111 1111
    n++;           // 1111 1111 --> 1 0000 0000
    

    There’s one bit in the ninth position, which represents 2^8, or 256, which is indeed the next largest power of 2. Each of the shifts overlaps all of the existing 1 bits in the number with some of the previously untouched zeroes, eventually producing a number of 1 bits equal to the number of bits in the original number. Adding one to that value produces a new power of 2.

    Another example; we’ll use 131, which is 10000011 in binary:

    n--;           // 1000 0011 --> 1000 0010
    n |= n >> 1;   // 1000 0010 | 0100 0001 = 1100 0011
    n |= n >> 2;   // 1100 0011 | 0011 0000 = 1111 0011
    n |= n >> 4;   // 1111 0011 | 0000 1111 = 1111 1111
    n |= n >> 8;   // ... (At this point all bits are 1, so further bitwise-or
    n |= n >> 16;  //      operations produce no effect.)
    n++;           // 1111 1111 --> 1 0000 0000
    

    And indeed, 256 is the next highest power of 2 from 131.

    If the number of bits used to represent the integer is itself a power of 2, you can continue to extend this technique efficiently and indefinitely (for example, add a n >> 32 line for 64-bit integers).

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

Sidebar

Ask A Question

Stats

  • Questions 193k
  • Answers 193k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using the built in libraries for asp.net (System.Runtime.Serialization and System.ServiceModel.Web)… May 12, 2026 at 6:34 pm
  • Editorial Team
    Editorial Team added an answer Reporting Services are very different between 2005 and 2008. You'll… May 12, 2026 at 6:34 pm
  • Editorial Team
    Editorial Team added an answer You need three tables: one to hold the baskets, one… May 12, 2026 at 6:34 pm

Related Questions

I have a table with index (autoincrement) and integer value. The table is millions
I'm practising the Switch Loop in a program. And I'm making a code where
I have a header.html which start with session_start();. Then the following code, $_SESSION['cart'][$sw_id] is
I have a problem with my Java progam suddenly exiting, without any exception thrown

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.