DEV Community

Discussion on: How to check if a number is a power of two for O(1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Surely any number is a power of 2?

2log2x=x 2^{\log_2 x}=x

Your code is checking for integer powers of 2
Collapse
 
ytskk profile image
Andrey • Edited

Hello, dont correctly understand about log. It can be simplified to x = x, and?

This method only about integer power numbers, check the input number type.

If you could found way to work with float , let us know)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

The input number type does not tell you that this is checking for integer powers. To make the intent of your function clearer, it would be better named something like isIntegerPowOfTwo

Thread Thread
 
ytskk profile image
Andrey

Thanks, let it be