-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Is power of two #358
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
Merged
Is power of two #358
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c708a9e
feat: is power of two algorithm
i-redbyte 75e936d
fix: add doc and rename files
i-redbyte 371e4f7
fix: text for tests
i-redbyte 65908e7
fix: after review
i-redbyte 9536f2e
fix: after review2
i-redbyte f93f0c2
fix: move IsPowerOfTwoUseLog method to math package
i-redbyte 2485d3a
Merge branch 'master' into feat_is_power_of_two
siriak 8b4e64c
fix: rename IsPowerOfTwoUseLog
i-redbyte cfad6de
Merge remote-tracking branch 'origin/feat_is_power_of_two' into feat_…
i-redbyte a5ca39d
fix: create checkisnumberpoweroftwo_test
i-redbyte 46dd724
Merge branch 'master' into feat_is_power_of_two
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// checkisnumberpoweroftwo.go | ||
// description: Is the number a power of two | ||
// details: | ||
// Checks if a number is a power of two- [Power of two](https://en.wikipedia.org/wiki/Power_of_two) | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see checkisnumberpoweroftwo_test.go | ||
|
||
package binary | ||
|
||
// IsPowerOfTwo This function uses the fact that powers of 2 are represented | ||
// like 10...0 in binary, and numbers one less than the power of 2 | ||
// are represented like 11...1. | ||
// Therefore, using the and function: | ||
// 10...0 | ||
// & 01...1 | ||
// 00...0 -> 0 | ||
// This is also true for 0, which is not a power of 2, for which we | ||
// have to add and extra condition. | ||
func IsPowerOfTwo(x int) bool { | ||
return x > 0 && (x&(x-1)) == 0 | ||
} | ||
|
||
// IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number | ||
// by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, | ||
// which in decimal system is 8 or = 2 * 2 * 2 | ||
func IsPowerOfTwoLeftShift(number uint) bool { | ||
if number == 0 { | ||
return false | ||
} | ||
for p := uint(1); p > 0; p = p << 1 { | ||
if number == p { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// checkisnumberpoweroftwo_test.go | ||
// description: Test for Is the number a power of two | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see checkisnumberpoweroftwo.go | ||
|
||
package binary | ||
|
||
import ( | ||
math2 "github.com/TheAlgorithms/Go/math" | ||
"math" | ||
"testing" | ||
) | ||
|
||
func getTestsForPowerOfTwo() []struct { | ||
name string | ||
a int | ||
missing bool | ||
} { | ||
var tests = []struct { | ||
name string | ||
a int | ||
missing bool | ||
}{ | ||
{"Is 64 a power of 2? - YES", 64, true}, | ||
{"Is 1 a power of 2? - YES", 1, true}, | ||
{"Is 2 a power of 2? - YES", 2, true}, | ||
{"Is 5 a power of 2? - NO", 5, false}, | ||
{"Is 1023 a power of 2? - NO", 1023, false}, | ||
{"Is 1024 a power of 2? - YES", 1024, true}, | ||
{"Is 0 a power of 2? - NO", 0, false}, | ||
{"Is 9223372036854775807 a power of 2? - NO", math.MaxInt64, false}, | ||
{"Is 9223372036854775806 a power of 2? - NO", math.MaxInt64, false}, | ||
{"Is 4611686018427387904 a power of 2? - YES", 4611686018427387904, true}, | ||
} | ||
return tests | ||
} | ||
|
||
func TestIsPowerOfTwo(t *testing.T) { | ||
tests := getTestsForPowerOfTwo() | ||
for _, tv := range tests { | ||
t.Run(tv.name, func(t *testing.T) { | ||
result := IsPowerOfTwo(tv.a) | ||
t.Log(tv.a, " ", result) | ||
if result != tv.missing { | ||
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsPowerOfTwoLeftShift(t *testing.T) { | ||
tests := getTestsForPowerOfTwo() | ||
for _, tv := range tests { | ||
t.Run(tv.name, func(t *testing.T) { | ||
result := IsPowerOfTwoLeftShift(uint(tv.a)) | ||
t.Log(tv.a, " ", result) | ||
if result != tv.missing { | ||
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsPowOfTwoUseLog(t *testing.T) { | ||
tests := getTestsForPowerOfTwo() | ||
for _, tv := range tests { | ||
t.Run(tv.name, func(t *testing.T) { | ||
result := math2.IsPowOfTwoUseLog(float64(tv.a)) | ||
t.Log(tv.a, " ", result) | ||
if result != tv.missing { | ||
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkIsPowerOfTwoBinaryMethod(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
IsPowerOfTwo(1024) | ||
} | ||
} | ||
|
||
func BenchmarkIsPowerOfTwoUseCycleAndLeftShift(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
IsPowerOfTwoLeftShift(1024) | ||
} | ||
} | ||
|
||
func BenchmarkIsPowerOfTwoUseLog(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
math2.IsPowOfTwoUseLog(1024) | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package math | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
// IsPowOfTwoUseLog This function checks if a number is a power of two using the logarithm. | ||
// The limiting degree can be from 0 to 63. | ||
// See alternatives in the binary package. | ||
func IsPowOfTwoUseLog(number float64) bool { | ||
if number == 0 || math.Round(number) == math.MaxInt64 { | ||
return false | ||
} | ||
log := math.Log2(number) | ||
return log == math.Round(log) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// checkisnumberpoweroftwo_test.go | ||
// description: Test for Is the number a power of two | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see checkisnumberpoweroftwo.go | ||
|
||
package math | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func getTestsForPowerOfTwo() []struct { | ||
name string | ||
a int | ||
missing bool | ||
} { | ||
var tests = []struct { | ||
name string | ||
a int | ||
missing bool | ||
}{ | ||
{"Is 64 a power of 2? - YES", 64, true}, | ||
{"Is 1 a power of 2? - YES", 1, true}, | ||
{"Is 2 a power of 2? - YES", 2, true}, | ||
{"Is 5 a power of 2? - NO", 5, false}, | ||
{"Is 1023 a power of 2? - NO", 1023, false}, | ||
{"Is 1024 a power of 2? - YES", 1024, true}, | ||
{"Is 0 a power of 2? - NO", 0, false}, | ||
{"Is 9223372036854775807 a power of 2? - NO", math.MaxInt64, false}, | ||
{"Is 9223372036854775806 a power of 2? - NO", math.MaxInt64, false}, | ||
{"Is 4611686018427387904 a power of 2? - YES", 4611686018427387904, true}, | ||
} | ||
return tests | ||
} | ||
|
||
func TestIsPowOfTwoUseLog(t *testing.T) { | ||
tests := getTestsForPowerOfTwo() | ||
for _, tv := range tests { | ||
t.Run(tv.name, func(t *testing.T) { | ||
result := IsPowOfTwoUseLog(float64(tv.a)) | ||
t.Log(tv.a, " ", result) | ||
if result != tv.missing { | ||
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkIsPowerOfTwoUseLog(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
IsPowOfTwoUseLog(1024) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.