changed shifting logic

This commit is contained in:
Gowtham Gopalakrishnan
2019-02-17 12:13:57 +05:30
parent f7396abfab
commit be9178df09

View File

@@ -2,7 +2,6 @@ package multibase
import ( import (
"fmt" "fmt"
"math"
"strconv" "strconv"
"strings" "strings"
) )
@@ -18,15 +17,12 @@ func binaryEncodeToString(src []byte) string {
// encodeBinary takes the src and dst bytes and converts each // encodeBinary takes the src and dst bytes and converts each
// byte to their binary rep using power reduction method // byte to their binary rep using power reduction method
func encodeBinary(dst []byte, src []byte) { func encodeBinary(dst []byte, src []byte) {
for i := 0; i < len(src); i++ { for i, b := range src {
t := src[i] for j := 0; j < 8; j++ {
for j := i << 3; j < (i<<3)+8; j++ { if b&(1<<uint(7-j)) == 0 {
higherPower := math.Pow(2, float64(7-(j&7))) dst[i*8+j] = '0'
if t >= byte(higherPower) {
dst[j] = '1'
t = t - byte(higherPower)
} else { } else {
dst[j] = '0' dst[i*8+j] = '1'
} }
} }
} }