forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: algorithm: Bitwise log base 2 (TheAlgorithms#503)
* feat: bitwise min * fix: bitwise min, change for vararg * fix: change min tests * fix: benchmark for bitwise * fix: rename tests * fix: add value param * fix: change condition * feat: added description to some functions * Updated Documentation in README.md * fix: change descriptions * Updated Documentation in README.md * Updated Documentation in README.md * Updated Documentation in README.md * feat: abs algo * Updated Documentation in README.md * fix: add comment * Updated Documentation in README.md * fix: new comment * Updated Documentation in README.md * fix: rename func and remove package * Updated Documentation in README.md * Update math/abs.go Co-authored-by: Taj <[email protected]> * Updated Documentation in README.md * Update math/binary/abs.go Co-authored-by: Taj <[email protected]> * Updated Documentation in README.md * fix: rename func and move binary test * add package description and add function name to comment * Updated Documentation in README.md * rename Intercept method to YIntercept * Updated Documentation in README.md * fix strings * Updated Documentation in README.md * fix commit * fix name cos tests and rename epsilon to epsilonCos * feat: sin * Updated Documentation in README.md * fix test for sin * fix epsilon * rename package to test * fix afte review * Updated Documentation in README.md * Updated Documentation in README.md * Updated Documentation in README.md * @feat bitwise logarithm * @fix: rename function name to LogBase2 Co-authored-by: Rak Laptudirm <[email protected]> Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Taj <[email protected]>
- Loading branch information
1 parent
ff22113
commit 603f2fd
Showing
3 changed files
with
102 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see logarithm_test.go | ||
|
||
package binary | ||
|
||
// LogBase2 Finding the exponent of n = 2**x using bitwise operations (logarithm in base 2 of n) [See more](https://en.wikipedia.org/wiki/Logarithm) | ||
func LogBase2(n uint32) uint32 { | ||
base := [5]uint32{0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000} | ||
exponents := [5]uint32{1, 2, 4, 8, 16} | ||
var result uint32 | ||
for i := 4; i >= 0; i-- { | ||
if n&base[i] != 0 { | ||
n >>= exponents[i] | ||
result |= exponents[i] | ||
} | ||
} | ||
return result | ||
} |
This file contains 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,48 @@ | ||
// logarithm_test.go | ||
// description: Test for finding the exponent of n = 2**x using bitwise operations (logarithm in base 2 of n) | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see logarithm.go | ||
|
||
package binary | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func TestLogBase2(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
n uint32 | ||
want uint32 | ||
}{ | ||
{"log2(1) = 0", 1, 0}, | ||
{"log2(2) = 1", 2, 1}, | ||
{"log2(4) = 2", 4, 2}, | ||
{"log2(8) = 3", 8, 3}, | ||
{"log2(16) = 4", 16, 4}, | ||
{"log2(32) = 5", 32, 5}, | ||
{"log2(64) = 6", 64, 6}, | ||
{"log2(128) = 7", 128, 7}, | ||
{"log2(256) = 8", 256, 8}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if got := LogBase2(test.n); got != test.want { | ||
t.Errorf("LogBase2() = %v, want %v", got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkBitwiseLogBase2(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
LogBase2(1024) | ||
} | ||
} | ||
|
||
func BenchmarkMathPAckageLogBase2(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
math.Log2(1024) | ||
} | ||
} |