0

This question is from job interview...
Use this template to write a C++ function that checks if a positive integer is a power of two.

bool p(int n) { return ********; } 

You have to replace the 8 '*' symbols with other symbols in order to make the function work correctly.
My best approach was this:

bool p(int n) { return !(n&=n-1); } 

Unfortunately it was wrong, because there are 9 symbols here...
Any ideas?

1

1 Answer 1

0

Why assign to n? Just remove = and you have one character less. It will create a temporary instead, the logic won't change.

Sign up to request clarification or add additional context in comments.

3 Comments

well, I thought '&' had a higher priority than '-' Thank you!
Then replace that eight stars with call to the function from the duplicate :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.