Power of Two Check
Check whether a number is a power of two using bit manipulation.
Sample input
16
Sample output
true
Solution
n = 16
print(n > 0 and (n & (n - 1)) == 0)
const n = 16;
console.log(n > 0 && (n & (n - 1)) === 0);
public class Main {
public static void main(String[] args) {
int n = 16;
System.out.println(n > 0 && (n & (n - 1)) == 0);
}
}
fun main() {
val n = 16
println(n > 0 && (n and (n - 1)) == 0)
}
let n = 16
print(n > 0 && (n & (n - 1)) == 0)
void main() {
int n = 16;
print(n > 0 && (n & (n - 1)) == 0);
}
#include <iostream>
using namespace std;
int main() {
int n = 16;
cout << ((n > 0 && (n & (n - 1)) == 0) ? "true" : "false") << endl;
return 0;
}
#include <stdio.h>
int main() {
int n = 16;
printf("%s\n", (n > 0 && (n & (n - 1)) == 0) ? "true" : "false");
return 0;
}