mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-02-04 05:06:59 +08:00
Check if number is already power of two
This commit is contained in:
@@ -2,10 +2,13 @@
|
||||
*
|
||||
* This script will find next power of two
|
||||
* of given number.
|
||||
* More about it:
|
||||
* https://www.techiedelight.com/round-next-highest-power-2/
|
||||
*
|
||||
*/
|
||||
|
||||
export const nextPowerOfTwo = (n) => {
|
||||
if (n > 0 && (n & (n - 1)) === 0) return n
|
||||
let result = 1
|
||||
while (n > 0) {
|
||||
result = result << 1
|
||||
|
||||
Reference in New Issue
Block a user