

First, notice that all intermediate products are powers of two. This example exposes a few properties common to all negative powers of two. For example, the number 0.00390625 requires 8 multiplications to reach 1, giving 2 -8: To convert a negative power of two into the form 2 n, count the number n of multiplications by 2 that it takes to reach a product of 1 - then negate n. In fact, the quotients form a descending sequence of consecutive nonnegative powers of two. This example exposes a property common to all positive powers of two: all intermediate quotients are powers of two as well. For example, the number 524,288 requires 19 divisions to reach 1, giving 2 19: The most straightforward way to convert a positive power of two into the form 2 n is to count the number n of divisions by 2 that it takes to reach a quotient of 1. The algorithm to use depends on whether you are working with a positive or negative power of two (you don’t need an algorithm to tell you that 1, which is 2 0, is a power of two). In general, to show that a number is a power of two, you’ll need a procedure - that is, an algorithm - to put it into the form 2 n.

But what about powers of two you have not memorized? If you’re computer savvy, you’ll recognize other powers of two, like 256, 1,024, 4,096, and even 65,536. Some numbers you’ll recognize immediately as powers of two, despite not being expressed in the form 2 n. (If you’re looking for information about how to tell if a number in a computer is a power of two, see my articles “What Powers of Two Look Like Inside a Computer” and “Ten Ways to Check if an Integer Is a Power Of Two in C”.) Transforming a Power of Two Into the Form 2 n Then, we’ll see how to test whether an arbitrary number is a power of two. First, we’ll see how to transform known powers of two, using simple arithmetic, into the form 2 n. But what about arbitrary positive numbers like 16,392, 524,288, or 0.00390625? Are they powers of two? Here’s how to tell - if they can be simplified to the form 2 n, they are if they can’t, they’re not.

For example, 2 12, 2 0, and 2 -37 are powers of two. That’s easy if it’s in the form 2 n, where n is an integer. Try to run these examples on your machine and drop one comment below if you have any queries.How can you tell if a number is a power of two? We have learned two different ways to verify if a number is the power of two or not. It will give the same result for a number as the first solution. if the value of count is 1 or not as per the second solution. In the previous solution, we were checking if the AND of a number and its previous number is 0 or not i.e. If the value of count is greater than 1 in the while loop, we can just skip and say that this is not a power of two. # include using namespace std int main ( ) Īctually, this is the same as the first method.
