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: [email protected] 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\n",ret); 28 29 n = 16; 30 ret = isPowerOfTwo(n); 31 printf("%d\n",ret); 32 33 return 0; 34 }
时间: 2024-12-20 10:56:47