answersLogoWhite

0


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

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Give a one-line C expression to test whether a number is a power of 2?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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

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


A number or expression that is raised to a power?

base


What is the number or expression in a power that is used as a factor in repeated multiplication?

Base of a power


The expression 10 to the zero power is equal to what number?

the answer is 10^0=1 the number 1


A number or symbol placed to the right of and above another number symbol or expression?

A power or exponent.


What is the definition of exponents?

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


What is a number that indicates how many times a number or expression is to be multiplied by itself?

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 ..."


Is the expression 125649 to the 0 power equal to 1 to the 125649 power a true statement?

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


What is a number or symbol placed to the right of and above another another number symbol or expression?

It is the power, index or exponent.


What are the parts of an expression written in scientific notation?

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.


What is an algebraic expresion?

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,


A product formed from repeated multiplication by the same number or expression It consists of a base and an exponent?

power.