Best Answer

The one line expression is:

((0 != n) && !(n & n-1))

example:

int main ()

{

for (int n = 0; n <= 1000001; ++n)

{

if ((0 != n) && !(n & n-1))

cout << n << " is a power of 2" << endl;

}

return 0;

}

will produce:

1 is a power of 2

2 is a power of 2

4 is a power of 2

8 is a power of 2

16 is a power of 2

32 is a power of 2

64 is a power of 2

128 is a power of 2

256 is a power of 2

512 is a power of 2

1024 is a power of 2

2048 is a power of 2

4096 is a power of 2

8192 is a power of 2

16384 is a power of 2

32768 is a power of 2

65536 is a power of 2

131072 is a power of 2

262144 is a power of 2

524288 is a power of 2

Q: Give a one-line C expression to test whether a number is a power of 2?

Write your answer...

Submit

Still have questions?

Continue Learning about Math & Arithmetic

Base of a power

A power or exponent.

the answer is 10^0=1 the number 1

a quantity representing the power to which a given number or expression is to be raised, usually expressed as a raised symbol beside the number or expression

It is called a power. If the power is 2, it is read as "squared and if the power is 3 it is read as "cubed", otherwise is is read as "to the power of ..."

Related questions

What is the number or expression in a power that is multiplied by itself

base

Base of a power

A power or exponent.

the answer is 10^0=1 the number 1

a quantity representing the power to which a given number or expression is to be raised, usually expressed as a raised symbol beside the number or expression

It is called a power. If the power is 2, it is read as "squared and if the power is 3 it is read as "cubed", otherwise is is read as "to the power of ..."

I suggest you calculate both sides, then check whether they are equal.

It is the power, index or exponent.

An expression in scientific notation consists of two parts: the coefficient and the power of 10. The coefficient is a number between 1 and 10, representing the significant digits of the number. The power of 10 indicates the magnitude of the number.

an algebraic expression is an expression built up from constants, variables, and a finite number of algebraic operations (addition, subtraction, multiplication,division and exponentiation to a power that is a rational number). For example,

power.