LeetCode 231

Power of Two

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

 1 /*************************************************************************
 2     > File Name: LeetCode231.c
 3     > Author: Juntaran    
 4     > Mail: Jacinthmail@gmail.com
 5     > Created Time: 2016年05月10日 星期二 02时55分46秒
 6  ************************************************************************/
 7 
 8 /*************************************************************************
 9     
10     Power of Two
11     
12     Given an integer, write a function to determine if it is a power of two.
13 
14  ************************************************************************/
15 
16 #include "stdio.h"
17 
18 int isPowerOfTwo(int n) {
19     int temp = (n>0 && !(n&(n-1)));
20     return temp;
21 }
22 
23 int main()
24 {
25     int n = 9;
26     int ret = isPowerOfTwo(n);
27     printf("%d
",ret);
28     
29     n = 16;
30     ret = isPowerOfTwo(n);
31     printf("%d
",ret);
32     
33     return 0;
34 }
原文地址:https://www.cnblogs.com/Juntaran/p/5479097.html