mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
19 lines
345 B
JavaScript
19 lines
345 B
JavaScript
/**
|
|
*
|
|
* 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
|
|
n = n >> 1
|
|
}
|
|
return result
|
|
}
|