Skip to content

Commit 372348f

Browse files
committed
add algorithm that calculates next power of two
1 parent 5f601fa commit 372348f

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

Bit-Manipulation/NextPowerOfTwo.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
* This script will find next power of two
4+
* of given number.
5+
*
6+
*/
7+
8+
export const nextPowerOfTwo = (n) => {
9+
let result = 1
10+
while (n > 0) {
11+
result = result << 1
12+
n = n >> 1
13+
}
14+
return result
15+
}

Bit-Manipulation/test/IsPowerOfTwo.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {IsPowerOfTwo} from '../IsPowerOfTwo'
1+
import { IsPowerOfTwo } from '../IsPowerOfTwo'
22

33
test('Check if 0 is a power of 2 or not:', () => {
44
const res = IsPowerOfTwo(0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { nextPowerOfTwo } from '../NextPowerOfTwo'
2+
3+
describe('NextPowerOfTwo', () => {
4+
it.each`
5+
input | result
6+
${0} | ${1}
7+
${1} | ${2}
8+
${2} | ${4}
9+
${3} | ${4}
10+
${5} | ${8}
11+
${125} | ${128}
12+
${1024} | ${2048}
13+
${10000} | ${16384}
14+
`('returns $result when is given $input', ({ input, result }) => {
15+
const res = nextPowerOfTwo(input)
16+
expect(res).toBe(result)
17+
})
18+
})

Conversions/TemperatureConversion.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ const fahrenheitToRankine = (fahrenheit) => {
4040
const kelvinToCelsius = (kelvin) => {
4141
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
4242
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
43-
return Math.round((kelvin) - 273.15)
44-
43+
return Math.round((kelvin) - 273.15)
4544
}
4645

4746
const kelvinToFahrenheit = (kelvin) => {
@@ -53,7 +52,7 @@ const kelvinToFahrenheit = (kelvin) => {
5352
const kelvinToRankine = (kelvin) => {
5453
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
5554
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
56-
return Math.round(( (kelvin) * 9 / 5))
55+
return Math.round(((kelvin) * 9 / 5))
5756
}
5857

5958
const rankineToCelsius = (rankine) => {

0 commit comments

Comments
 (0)