forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bitwise max (TheAlgorithms#347)
* feat: add an algorithm for finding the maximum number without using conditional operators
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// bitwiseMax.go | ||
// description: Gives max of two integers | ||
// details: | ||
// implementation of finding the maximum of two numbers using only binary operations without using conditions | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see bitwiseMax_test.go | ||
|
||
package max | ||
|
||
func BitwiseMax(a int, b int, base int) int { | ||
z := a - b | ||
i := (z >> base) & 1 | ||
return a - (i * z) | ||
} |
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,77 @@ | ||
// bitwiseMax_test.go | ||
// description: Test for BitwiseMax | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see bitwiseMax.go | ||
|
||
package max | ||
|
||
import "testing" | ||
|
||
func TestBitwiseMax(t *testing.T) { | ||
base32 := 31 | ||
|
||
t.Run("Testing(32bit) a = 32 and m = 64: ", func(t *testing.T) { | ||
max := BitwiseMax(32, 64, base32) | ||
if max != 64 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
t.Run("Testing(32bit) a = 1024 and m = -9: ", func(t *testing.T) { | ||
max := BitwiseMax(1024, -9, base32) | ||
if max != 1024 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
t.Run("Testing(32bit) a = -6 and m = -6: ", func(t *testing.T) { | ||
max := BitwiseMax(-6, -6, base32) | ||
if max != -6 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
t.Run("Testing(32bit) a = -72 and m = -73: ", func(t *testing.T) { | ||
max := BitwiseMax(-72, -73, base32) | ||
if max != -72 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
base64 := 63 | ||
t.Run("Testing(64bit) a = 32 and m = 9223372036854775807: ", func(t *testing.T) { | ||
max := BitwiseMax(32, 9223372036854775807, base64) | ||
if max != 9223372036854775807 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
t.Run("Testing(64bit) a = 1024 and m = -9223372036854770001: ", func(t *testing.T) { | ||
max := BitwiseMax(1024, -9223372036854770001, base64) | ||
if max != 1024 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
t.Run("Testing(64bit) a = -6 and m = -6: ", func(t *testing.T) { | ||
max := BitwiseMax(-6, -6, base64) | ||
if max != -6 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
t.Run("Testing(64bit) a = -4223372036854775809 and m = -4223372036854775808: ", func(t *testing.T) { | ||
max := BitwiseMax(-4223372036854775809, -4223372036854775808, base64) | ||
if max != -4223372036854775808 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
|
||
base8 := 7 | ||
t.Run("Testing(8bit) a = 257 and m = 256: ", func(t *testing.T) { | ||
max := BitwiseMax(8, 16, base8) | ||
if max != 16 { | ||
t.Fatalf("Error: BitwiseMax returned bad value") | ||
} | ||
}) | ||
} |