Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Input: 16 Output: true
Input: 5 Output: false
Follow up: Could you solve it without loops/recursion?
impl Solution {
pub fn is_power_of_four(num: i32) -> bool {
let mut num = num;
if num <= 0 {
false
} else {
while num % 4 == 0 {
num /= 4;
}
num == 1
}
}
}
impl Solution {
pub fn is_power_of_four(num: i32) -> bool {
(num > 0) && (num & 0x55555555 == num) && (num & (num - 1) == 0)
}
}