Home > Backend Development > C++ > Is a Number a Power of 2? How Can We Efficiently Determine This?

Is a Number a Power of 2? How Can We Efficiently Determine This?

DDD
Release: 2025-01-29 19:26:12
Original
757 people have browsed it

Is a Number a Power of 2?  How Can We Efficiently Determine This?

Whether the number of numerals is 2 is the power of 2

Question statement

In computer science, a common problem is to determine whether the given number is the power of 2. This problem is especially in the applied and algorithm analysis. This algorithm should have the following characteristics:

The code is simple and clear.
  1. Can accurately handle any unsigned long integer value.
  2. Method
Method 1: Method of the power of the shift

A method to solve this problem is to iterate 2 continuous power, check whether any power -in -one matches the input number. This can be implemented as follows: Method 2: The number of calculation algorithms

Another method involves the calculation of the use of the number of pairs. However, this requires caution because floating -point calculations may cause accuracy. Consider the following code:
<code class="language-c#">private bool IsPowerOfTwo(ulong number)
{
    if (number == 0)
        return false;

    for (ulong power = 1; power > 0; power <<= 1)
    {
        if (power == number)
            return true;
        if (power > number)
            return false;
    }
    return false;
}</code>
Copy after login

The optimal solution: bit computing method More effective solutions are the use of bit operations. The power of 2 has a unique characteristic: when the (X -1) is performed and the operation (&), the result value is always 0. This attribute can be expressed as follows:

In order to eliminate the candidate of the power of zero as 2, it can be modified slightly:

<code class="language-c#">private bool IsPowerOfTwo_2(ulong number)
{
    double log = Math.Log(number, 2);
    double pow = Math.Pow(2, Math.Round(log));
    return pow == number;
}</code>
Copy after login

Explanation

Check the position of each bit of the binary representation of X and (X -1) by the position and the operation (&). If both are 1, the result is 1; otherwise, the result of 0.2 is only set to 1 in its binary representation. If you minus them 1, only the one on the far right will be changed to 0 to 0 Essence Therefore, the power of 2 is 0 with the (X -1) position and the calculation result of 0.

This method provides an efficient and direct solution to determine whether the number is 2.
<code class="language-c#">bool IsPowerOfTwo(ulong x)
{
    return (x & (x - 1)) == 0;
}</code>
Copy after login

The above is the detailed content of Is a Number a Power of 2? How Can We Efficiently Determine This?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template