题目
Given an integer, write a function to determine if it is a power of two.
Example 1:1
2
3Input: 1
Output: true
Explanation: 20 = 1
Example 2:1
2
3Input: 16
Output: true
Explanation: 24 = 16
Example 3:1
2Input: 218
Output: false
分析
这是一个求2的N次方的题目,较为基础.
第一种解题方法:直接求是不是即可(暴力判断);
第二种解法:位运算,判断所有位数上仅有一位是1其他的是0即可求得。
C语言
1 | #include<stdio.h> |