Skip to content

Commit

Permalink
feat: add bitwise max (TheAlgorithms#347)
Browse files Browse the repository at this point in the history
* feat: add an algorithm for finding the maximum number without using conditional operators
  • Loading branch information
i-redbyte authored Sep 17, 2021
1 parent 63400d5 commit bcb29db
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
14 changes: 14 additions & 0 deletions math/max/bitwiseMax.go
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)
}
77 changes: 77 additions & 0 deletions math/max/bitwiseMax_test.go
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")
}
})
}

0 comments on commit bcb29db

Please sign in to comment.