Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add code for checking whether given number is a power of 2 or not #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 3 commits into TheAlgorithms:master from Aayushi-Mittal:powerOf2
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Bit-Manipulation/IsPowerOfTwo.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
author: @Aayushi-Mittal

This script will check whether the given
number is a power of two or not.

A number will be a power of two if only one bit is set and rest are unset.
This is true for all the cases except 01 because (2^0 = 1) which is not a power of 2.
For eg: 10 (2^1 = 2), 100 (2^2 = 4), 10000 (2^4 = 16)

Reference Link: https://www.hackerearth.com/practice/notes/round-a-number-to-the-next-power-of-2/

If we will subtract 1 from a number that is a power of 2 we will get it's 1's complement.
And we know that 1's complement is just opp. of that number.
So, (n & (n-1)) will be 0.

For eg: (1000 & (1000-1))
1 0 0 0 // Original Number (8)
0 1 1 1 // After Subtracting 1 (8-1 = 7)
_______
0 0 0 0 // will become 0

*/

export const IsPowerOfTwo = (n) => {
if (n != 0 && (n & (n - 1)) == 0) return true
else return false
}
26 changes: 26 additions & 0 deletions Bit-Manipulation/test/IsPowerOfTwo.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {IsPowerOfTwo} from '../IsPowerOfTwo'

test('Check if 0 is a power of 2 or not:', () => {
const res = IsPowerOfTwo(0)
expect(res).toBe(false)
})

test('Check if 0 is a power of 2 or not:', () => {
const res = IsPowerOfTwo(1)
expect(res).toBe(false)
})

test('Check if 4 is a power of 2 or not:', () => {
const res = IsPowerOfTwo(4)
expect(res).toBe(true)
})

test('Check if 1024 is a power of 2 or not:', () => {
const res = IsPowerOfTwo(1024)
expect(res).toBe(true)
})

test('Check if 1025 is a power of 2 or not:', () => {
const res = IsPowerOfTwo(1025)
expect(res).toBe(false)
})
1 change: 1 addition & 0 deletions DIRECTORY.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

## Bit-Manipulation
* [BinaryCountSetBits](https://github.com/TheAlgorithms/Javascript/blob/master/Bit-Manipulation/BinaryCountSetBits.js)
* [IsPowerOfTwo](https://github.com/TheAlgorithms/Javascript/blob/master/Bit-Manipulation/IsPowerOfTwo.js)
* [SetBit](https://github.com/TheAlgorithms/Javascript/blob/master/Bit-Manipulation/SetBit.js)

## Cache
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /