I need to output: yes
if N
is a direct power of 2
and no
otherwise, where 1 <= N <= 1e100
Same in Python code:
n=int(input())
if(n!=0 and (n&(n-1)==0)):
print("yes")
else:
print("no")
I need to output: yes
if N
is a direct power of 2
and no
otherwise, where 1 <= N <= 1e100
Same in Python code:
n=int(input())
if(n!=0 and (n&(n-1)==0)):
print("yes")
else:
print("no")
You can use BigInteger
to store arbitrarily large integers.
Scanner sc = new Scanner(System.in);
BigInteger n = new BigInteger(sc.nextLine());
if(!n.equals(BigInteger.ZERO) && n.and(n.subtract(BigInteger.ONE)).equals(BigInteger.ZERO)){
System.out.println("yes");
} else {
System.out.println("no");
}
In Java, you have to use the BigInteger
class for integers this big. This class also has several convenient methods to work with the binary representation of this number.
For example, you can use bitCount
to determine if number is a power of 2:
boolean isPowerOf2(BigInteger integer) {
return integer.bitCount() == 1;
}
Usage:
var integer = new BigInteger("1267650600228229401496703205376");
if (isPowerOf2(integer)) System.out.println("yarp");
else System.out.println("nope");