Skip to content

Commit

Permalink
# ADD - decimal to binary
Browse files Browse the repository at this point in the history
  • Loading branch information
morishjs committed Jun 17, 2019
1 parent ad74066 commit b40d50b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/interger_to_bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
)

func decimalToBinary(val uint64) []uint64 {
bits := []uint64{}

for val != 0 {
mod := val % 2
bits = append(bits, mod)

val = val / 2
}

for i := 0; i < len(bits); i++ {
fmt.Print(bits)
}

return bits
}

35 changes: 35 additions & 0 deletions src/interger_to_bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"reflect"
"testing"
)

func Test_decimalToBinary(t *testing.T) {
type args struct {
val uint64
}
tests := []struct {
name string
args args
want []uint64
}{
{
"decimalToBinary#1",
args{3},
[]uint64{1,1},
},
{
"decimalToBinary#2",
args{15},
[]uint64{1,1,1,1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := decimalToBinary(tt.args.val); !reflect.DeepEqual(got, tt.want) {
t.Errorf("decimalToBinary() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit b40d50b

Please sign in to comment.