From 372348f64c03d7e358cfd81807d23917705d37c2 Mon Sep 17 00:00:00 2001 From: Indre Maksimovaite Date: Mon, 11 Oct 2021 20:13:36 +0300 Subject: [PATCH 1/2] add algorithm that calculates next power of two --- Bit-Manipulation/NextPowerOfTwo.js | 15 +++++++++++++++ Bit-Manipulation/test/IsPowerOfTwo.test.js | 2 +- Bit-Manipulation/test/NextPowerOfTwo.test.js | 18 ++++++++++++++++++ Conversions/TemperatureConversion.js | 5 ++--- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 Bit-Manipulation/NextPowerOfTwo.js create mode 100644 Bit-Manipulation/test/NextPowerOfTwo.test.js diff --git a/Bit-Manipulation/NextPowerOfTwo.js b/Bit-Manipulation/NextPowerOfTwo.js new file mode 100644 index 000000000..b5999252f --- /dev/null +++ b/Bit-Manipulation/NextPowerOfTwo.js @@ -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 +} diff --git a/Bit-Manipulation/test/IsPowerOfTwo.test.js b/Bit-Manipulation/test/IsPowerOfTwo.test.js index 2ad0f3742..30539f5e5 100644 --- a/Bit-Manipulation/test/IsPowerOfTwo.test.js +++ b/Bit-Manipulation/test/IsPowerOfTwo.test.js @@ -1,4 +1,4 @@ -import {IsPowerOfTwo} from '../IsPowerOfTwo' +import { IsPowerOfTwo } from '../IsPowerOfTwo' test('Check if 0 is a power of 2 or not:', () => { const res = IsPowerOfTwo(0) diff --git a/Bit-Manipulation/test/NextPowerOfTwo.test.js b/Bit-Manipulation/test/NextPowerOfTwo.test.js new file mode 100644 index 000000000..271b08249 --- /dev/null +++ b/Bit-Manipulation/test/NextPowerOfTwo.test.js @@ -0,0 +1,18 @@ +import { nextPowerOfTwo } from '../NextPowerOfTwo' + +describe('NextPowerOfTwo', () => { + it.each` + input | result + ${0} | ${1} + ${1} | ${2} + ${2} | ${4} + ${3} | ${4} + ${5} | ${8} + ${125} | ${128} + ${1024} | ${2048} + ${10000} | ${16384} + `('returns $result when is given $input', ({ input, result }) => { + const res = nextPowerOfTwo(input) + expect(res).toBe(result) + }) +}) diff --git a/Conversions/TemperatureConversion.js b/Conversions/TemperatureConversion.js index 75a7d69aa..8bf9130b1 100644 --- a/Conversions/TemperatureConversion.js +++ b/Conversions/TemperatureConversion.js @@ -40,8 +40,7 @@ const fahrenheitToRankine = (fahrenheit) => { const kelvinToCelsius = (kelvin) => { // Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin // Wikipedia reference: https://en.wikipedia.org/wiki/Celsius - return Math.round((kelvin) - 273.15) - + return Math.round((kelvin) - 273.15) } const kelvinToFahrenheit = (kelvin) => { @@ -53,7 +52,7 @@ const kelvinToFahrenheit = (kelvin) => { const kelvinToRankine = (kelvin) => { // Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin // Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale - return Math.round(( (kelvin) * 9 / 5)) + return Math.round(((kelvin) * 9 / 5)) } const rankineToCelsius = (rankine) => { From d6364548aa2a1e3e4d58a27dbfb6752eec8d9029 Mon Sep 17 00:00:00 2001 From: Indre Maksimovaite Date: Mon, 11 Oct 2021 20:24:38 +0300 Subject: [PATCH 2/2] Check if number is already power of two --- Bit-Manipulation/NextPowerOfTwo.js | 3 +++ Bit-Manipulation/test/NextPowerOfTwo.test.js | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Bit-Manipulation/NextPowerOfTwo.js b/Bit-Manipulation/NextPowerOfTwo.js index b5999252f..623469e4d 100644 --- a/Bit-Manipulation/NextPowerOfTwo.js +++ b/Bit-Manipulation/NextPowerOfTwo.js @@ -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 diff --git a/Bit-Manipulation/test/NextPowerOfTwo.test.js b/Bit-Manipulation/test/NextPowerOfTwo.test.js index 271b08249..073224046 100644 --- a/Bit-Manipulation/test/NextPowerOfTwo.test.js +++ b/Bit-Manipulation/test/NextPowerOfTwo.test.js @@ -4,12 +4,12 @@ describe('NextPowerOfTwo', () => { it.each` input | result ${0} | ${1} - ${1} | ${2} - ${2} | ${4} + ${1} | ${1} + ${2} | ${2} ${3} | ${4} ${5} | ${8} ${125} | ${128} - ${1024} | ${2048} + ${1024} | ${1024} ${10000} | ${16384} `('returns $result when is given $input', ({ input, result }) => { const res = nextPowerOfTwo(input)