Skip to content

Commit

Permalink
Merge 439ba1c into 09090b0
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Nov 14, 2018
2 parents 09090b0 + 439ba1c commit e04ee3c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ problems from
* [Day 82](https://github.com/vaskoz/dailycodingproblem-go/issues/173)
* [Day 83](https://github.com/vaskoz/dailycodingproblem-go/issues/175)
* [Day 84](https://github.com/vaskoz/dailycodingproblem-go/issues/177)
* [Day 85](https://github.com/vaskoz/dailycodingproblem-go/issues/179)
14 changes: 14 additions & 0 deletions day85/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package day85

// MathematicalIf returns x if b=1 and y if b=0.
func MathematicalIf(x, y, b int32) int32 {
var xMask, yMask int32
xMask = b
xMask |= xMask << 1
xMask |= xMask << 2
xMask |= xMask << 4
xMask |= xMask << 8
xMask |= xMask << 16
yMask = ^xMask
return (x & xMask) | (y & yMask)
}
31 changes: 31 additions & 0 deletions day85/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package day85

import "testing"

var testcases = []struct {
x, y, b, expected int32
}{
{10, 20, 1, 10},
{10, 20, 0, 20},
{-20, -40, 1, -20},
{-20, -40, 0, -40},
{100, -200, 1, 100},
{100, -200, 0, -200},
}

func TestMathematicalIf(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := MathematicalIf(tc.x, tc.y, tc.b); tc.expected != result {
t.Errorf("x(%d),y(%d),b(%d),expected(%d),got(%d)", tc.x, tc.y, tc.b, tc.expected, result)
}
}
}

func BenchmarkMathematicalIf(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
MathematicalIf(tc.x, tc.y, tc.b)
}
}
}

0 comments on commit e04ee3c

Please sign in to comment.