0

Checking wether a number can be written as power of 2

What is the most efficient way to check weather a given number can be written as power of 2 or not ? If possible please give the program approach in C or Java Ex: 4 —> true 16 —> true 20 —> false 256 —> true

27th Mar 2022, 7:02 AM
Bharani Kudala
Bharani Kudala - avatar
3 Respuestas
+ 4
public class Program { public static void main(String[] args) { int num = 534; int pow2 = 1; while(pow2<num){ pow2 *= 2; } System.out.println(pow2==num) ; } }
27th Mar 2022, 8:09 AM
Oma Falk
Oma Falk - avatar
+ 1
Find log to base 2. Must be an integer. Convert to binary.Must have exactly one 1.
27th Mar 2022, 7:58 AM
Oma Falk
Oma Falk - avatar
0
Written as power of two or verified as power of two?
27th Mar 2022, 7:04 AM
Ipang