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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:53:12+00:00 2026-05-11T07:53:12+00:00

Today I needed a simple algorithm for checking if a number is a power

  • 0

Today I needed a simple algorithm for checking if a number is a power of 2.

The algorithm needs to be:

  1. Simple
  2. Correct for any ulong value.

I came up with this simple algorithm:

private bool IsPowerOfTwo(ulong number) {     if (number == 0)         return false;      for (ulong power = 1; power > 0; power = power << 1)     {         // This for loop used shifting for powers of 2, meaning         // that the value will become 0 after the last shift         // (from binary 1000...0000 to 0000...0000) then, the 'for'         // loop will break out.          if (power == number)             return true;         if (power > number)             return false;     }     return false; } 

But then I thought: How about checking if log2 x is an exactly a round number? When I checked for 2^63+1, Math.Log() returned exactly 63 because of rounding. So I checked if 2 to the power 63 is equal to the original number and it is, because the calculation is done in doubles and not in exact numbers.

private bool IsPowerOfTwo_2(ulong number) {     double log = Math.Log(number, 2);     double pow = Math.Pow(2, Math.Round(log));     return pow == number; } 

This returned true for the given wrong value: 9223372036854775809.

Is there a better algorithm?

  • 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. 2026-05-11T07:53:13+00:00Added an answer on May 11, 2026 at 7:53 am

    There’s a simple trick for this problem:

    bool IsPowerOfTwo(ulong x) {     return (x & (x - 1)) == 0; } 

    Note, this function will report true for 0, which is not a power of 2. If you want to exclude that, here’s how:

    bool IsPowerOfTwo(ulong x) {     return (x != 0) && ((x & (x - 1)) == 0); } 

    Explanation

    First and foremost the bitwise binary & operator from MSDN definition:

    Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

    Now let’s take a look at how this all plays out:

    The function returns boolean (true / false) and accepts one incoming parameter of type unsigned long (x, in this case). Let us for the sake of simplicity assume that someone has passed the value 4 and called the function like so:

    bool b = IsPowerOfTwo(4) 

    Now we replace each occurrence of x with 4:

    return (4 != 0) && ((4 & (4-1)) == 0); 

    Well we already know that 4 != 0 evals to true, so far so good. But what about:

    ((4 & (4-1)) == 0) 

    This translates to this of course:

    ((4 & 3) == 0) 

    But what exactly is 4&3?

    The binary representation of 4 is 100 and the binary representation of 3 is 011 (remember the & takes the binary representation of these numbers). So we have:

    100 = 4 011 = 3 

    Imagine these values being stacked up much like elementary addition. The & operator says that if both values are equal to 1 then the result is 1, otherwise it is 0. So 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0, and 0 & 1 = 0. So we do the math:

    100 011 ---- 000 

    The result is simply 0. So we go back and look at what our return statement now translates to:

    return (4 != 0) && ((4 & 3) == 0); 

    Which translates now to:

    return true && (0 == 0); 
    return true && true; 

    We all know that true && true is simply true, and this shows that for our example, 4 is a power of 2.

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

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • 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 var instance : MyClass = Type.createInstance(Type.resolveClass("path.to.MyClass"), []); Few notes: resolveClass()… May 16, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer What you are seeing is the difference between using Enumerable.Sum… May 16, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer The direction I would start in would be developing a… May 16, 2026 at 5:46 pm

Trending Tags

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

Top Members

Related Questions

Today I needed to parse some data out from an xlsx file (Office open
I needed to strip the Chinese out of a bunch of strings today and
As today's code is getting more complex by the minute, code needs to be
So, I found this quote today, can anyone explain? If you think C++ is
I was greeted with a nasty bug today. The task is pretty trivial, all
I was learning about using the command line version of latex today, and I
RPO 1.0 (Runtime Page Optimizer) is a recently (today?) released component for ASP and
I am banging my head against the brick wall today. I am porting a
Greetings. I am trying to learn Java and Swing (today is my first day).
I was browsing a coworkers c# code today and found the following: using (MemoryStream

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.