Skip to content

Commit e216742

Browse files
committed
binary reversal resolved
1 parent 545dd70 commit e216742

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/easy/BinaryReversal.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
// TO-DO:
4+
// 1: Convert to binary
5+
// 2: pad to N * 8
6+
// 3: Reverse the binary
7+
// 4: Convert to decimal
8+
9+
import (
10+
"fmt"
11+
"strconv"
12+
)
13+
14+
func BinaryReversal(val int64) int64 {
15+
binVal := strconv.FormatInt(val, 2)
16+
17+
for(len(binVal)<8) {
18+
binVal = string("0") + binVal
19+
}
20+
21+
result := ""
22+
for _,v := range binVal {
23+
result = string(v) + result
24+
}
25+
26+
strVal, _ := strconv.ParseInt(result, 2, 64)
27+
fmt.Println(result)
28+
return strVal
29+
}
30+
31+
func main(){
32+
// Test cases
33+
result1 := BinaryReversal(47)
34+
fmt.Println(result1)
35+
}

0 commit comments

Comments
 (0)