mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
add algorithm that calculates next power of two
This commit is contained in:
15
Bit-Manipulation/NextPowerOfTwo.js
Normal file
15
Bit-Manipulation/NextPowerOfTwo.js
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
*
|
||||
* This script will find next power of two
|
||||
* of given number.
|
||||
*
|
||||
*/
|
||||
|
||||
export const nextPowerOfTwo = (n) => {
|
||||
let result = 1
|
||||
while (n > 0) {
|
||||
result = result << 1
|
||||
n = n >> 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user