## Power of Two

Given an integer, write a function to determine if it is a power of two.

### Analysis:

An straightforward way is to keep doing "divide by 2" for the number and check if current number "mod 2" is zero. However this is pretty slow (it still can pass the OJ).

More efficient way is to use bit manipulation.

If one number is a power of 2, then its binary must starts with 1 and all the lower bits are 0. e.g.,
2 10
4 100
8 1000
16 10000
...

Also, we have to find a "mask" to check this format, what "mask" should we use?
Let's see some examples:
1 01
3 011
7 0111
15 01111
...

Well, it is not hard to see that,  if one number "n" is a power of 2, then "n-1" must have all 1s in its binary format. Therefore, "n & n-1" must be 0.

### Code(C++):

class Solution {
public:
bool isPowerOfTwo(int n) {
return ( (n>0) && (n & (n-1))==0 ) || (n==1);
}
};


### Code(Python):

aclass Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
return n>0 and  n & (n-1)==0 or n==1


1. Thank You for posting it!

123 hp oj5746 printer support

2. Or can we take log base 2 of n, if ans is integer, that means it's power of 2.

3. Or can we take log base 2 of n, if ans is integer, that means it's power of 2.

4. Very nice and informative post

123 HP Officejet 4650

5. Hi, I am really exiting while reading your blog. It is very informative and easy to understand for all level skilled users.
Free Technical Support for HP Printers

6. very interesting , good job and thanks for sharing such a good blog.

123.hp.com/setup

7. I am really happy to say it’s an interesting post to read . I learned w information from your article , you are doing a great job . Thanks for a information

123.hp.com/setup 4625

8. Excellent post, thanks for this resource!

123 hp setup

9. Really its very useful information that you have shared and thanks for sharing the information with us.
Hp Officejet 5746 Troubleshooting

10. Very interesting,good job and thanks for sharing such a good

blog.your article is so convincing that I never stop myself to say

something about it.You’re doing a great job.Keep it up.

123 hp setup

11. I am looking for some good blog sites for studying. I was searching over search engines and found your blog site. Well i like your high quality blog site design plus your posting abilities. Keep doing it.

123 HP COM SETUP

12. Excellent solution for setup hp printers. Thanks for sharing. 123.hp.com/setup 4650

13. Excellent record 123.hp.com/setup 3755 Thank you for sharing.

14. Immense articles 123.hp.com/setup Thanks for sharing useful information.

15. 123.hp.com/setup Thanks for sharing useful information.

16. HP printer support 123.hp.com, 123.hp.com/setup

17. First-class articles for hp printer setup 123.hp.com, 123.hp.com/setup

18. First-class Structure 123.hp.com , First-rate Study 123.hp.com , Flattering Stuff 123.hp.com , Forceful Substance 123.hp.com , Forcible Summary 123.hp.com

19. Cheerful Training 123.hp.com , Cheering Unit 123.hp.com , Cheery Vignette 123.hp.com , Cherished Accepted 123.hp.com , Chic Acknowledgement 123.hp.com

20. Disciplined Judgement 123.hp.com , Distinct Key 123.hp.com , Diverting Knowledge 123.hp.com , Doable Leading 123.hp.com

21. A1 Advice 123.hp.com , Absolute Agenda 123.hp.com , Absolutely Aim 123.hp.com , Acceptable Anecode 123.hp.com , Accepted Apex 123.hp.com , Accepting Appreciable 123.hp.com

22. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.

HP Printer Setup