Skip to content

Commit 6a5f7a3

Browse files
committed
Add: Reverse Bits
1 parent c04a3a9 commit 6a5f7a3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: problems/reverse-bits/reverse_bits.go

+6
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
package reverse_bits
2+
3+
import "math/bits"
4+
5+
func reverseBits(num uint32) uint32 {
6+
return bits.Reverse32(num)
7+
}

Diff for: problems/reverse-bits/reverse_bits_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package reverse_bits
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input uint32
7+
expected uint32
8+
}
9+
10+
func TestReverseBits(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 43261596,
14+
expected: 964176192,
15+
},
16+
{
17+
input: 4294967293,
18+
expected: 3221225471,
19+
},
20+
}
21+
for _, tc := range tests {
22+
output := reverseBits(tc.input)
23+
if output != tc.expected {
24+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)