Skip to content

Commit 18451c6

Browse files
authored
feat: Binary to Decimal Conversion (#398)
1 parent 5f9d41b commit 18451c6

File tree

5 files changed

+100
-12
lines changed

5 files changed

+100
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
2525
* [Roman Numeral To Integer](./conversion/romantointeger.go)
2626
* [Integer To Roman Numeral](./conversion/integertoroman.go)
2727
* [Decimal To Binary](./conversion/decimaltobinary.go)
28+
* [Binary To Decimal](./conversion/binarytodecimal.go)
2829

2930
### Data Structures
3031
* [AVL Tree](./structure/avl/avl.go)

conversion/binarytodecimal.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Author: Motasim
3+
GitHub: https://github.com/motasimmakki
4+
Date: 19-Oct-2021
5+
*/
6+
7+
// This algorithm will convert any Binary number(0 or 1) to Decimal number(+ve number).
8+
// https://en.wikipedia.org/wiki/Binary_number
9+
// https://en.wikipedia.org/wiki/Decimal
10+
// Function receives a Binary Number as string and returns the Decimal number as integer.
11+
// Suppoted Binary number range is 0 to 2^(31-1).
12+
13+
// Package name.
14+
package conversion
15+
16+
// Importing necessary package.
17+
import (
18+
"errors"
19+
"regexp"
20+
)
21+
22+
var isValid = regexp.MustCompile("^[0-1]{1,}$").MatchString
23+
24+
// BinaryToDecimal() function that will take Binary number as string,
25+
// and return it's Decimal equivalent as integer.
26+
func BinaryToDecimal(binary string) (int, error) {
27+
if !isValid(binary) {
28+
return -1, errors.New("not a valid binary string")
29+
}
30+
if len(binary) > 32 {
31+
return -1, errors.New("binary number must be in range 0 to 2^(31-1)")
32+
}
33+
var result, base int = 0, 1
34+
for i := len(binary) - 1; i >= 0; i-- {
35+
if binary[i] == '1' {
36+
result += base
37+
}
38+
base *= 2
39+
}
40+
return result, nil
41+
}

conversion/binarytodecimal_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package conversion
2+
3+
import "testing"
4+
5+
var binaryTestCases = map[string]int{
6+
"0": 0, "1": 1, "10": 2, "11": 3, "100": 4,
7+
"101": 5, "110": 6, "111": 7, "1000": 8, "1001": 9,
8+
"1010": 10, "1011": 11, "1100": 12, "1101": 13, "1110": 14,
9+
"1111": 15, "10000": 16, "10001": 17, "10010": 18, "10011": 19,
10+
"10100": 20, "10101": 21, "10110": 22, "10111": 23, "11000": 24,
11+
"11001": 25, "11010": 26, "11011": 27, "11100": 28, "11101": 29,
12+
"11110": 30, "11111": 31, "100000": 32, "100001": 33, "100010": 34,
13+
"100011": 35, "100100": 36, "100101": 37, "100110": 38, "100111": 39,
14+
"101000": 40, "101001": 41, "101010": 42, "101011": 43, "101100": 44,
15+
"101101": 45, "101110": 46, "101111": 47, "110000": 48, "110001": 49,
16+
"110010": 50, "110011": 51, "110100": 52, "110101": 53, "110110": 54,
17+
"110111": 55, "111000": 56, "111001": 57, "111010": 58, "111011": 59,
18+
"111100": 60, "111101": 61, "111110": 62, "111111": 63, "1000000": 64,
19+
"1000001": 65, "1000010": 66, "1000011": 67, "1000100": 68, "1000101": 69,
20+
"1000110": 70, "1000111": 71, "1001000": 72, "1001001": 73, "1001010": 74,
21+
"1001011": 75, "1001100": 76, "1001101": 77, "1001110": 78, "1001111": 79,
22+
"1010000": 80, "1010001": 81, "1010010": 82, "1010011": 83, "1010100": 84,
23+
"1010101": 85, "1010110": 86, "1010111": 87, "1011000": 88, "1011001": 89,
24+
"1011010": 90, "1011011": 91, "1011100": 92, "1011101": 93, "1011110": 94,
25+
"1011111": 95, "1100000": 96, "1100001": 97, "1100010": 98, "1100011": 99,
26+
"1100100": 100,
27+
}
28+
29+
func TestBinaryToDecimal(t *testing.T) {
30+
for input, expected := range binaryTestCases {
31+
out, err := BinaryToDecimal(input)
32+
if err != nil {
33+
t.Errorf("BinaryToDecimal(%s) returned an error %s", input, err.Error())
34+
}
35+
if out != expected {
36+
t.Errorf("BinaryToDecimal(%s) = %d; want %d", input, out, expected)
37+
}
38+
}
39+
}
40+
41+
func BenchmarkBinaryToDecimal(b *testing.B) {
42+
b.ReportAllocs()
43+
for i := 0; i < b.N; i++ {
44+
_, _ = BinaryToDecimal("1100100")
45+
}
46+
}

conversion/decimaltobinary.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ import (
1818
"strconv"
1919
)
2020

21-
// Inverse() function that will take string,
22-
// and returns the inverse of that string.
23-
func Inverse(str string) string {
21+
// Reverse() function that will take string,
22+
// and returns the reverse of that string.
23+
func Reverse(str string) string {
2424
rStr := []rune(str)
2525
for i, j := 0, len(rStr)-1; i < len(rStr)/2; i, j = i+1, j-1 {
2626
rStr[i], rStr[j] = rStr[j], rStr[i]
2727
}
2828
return string(rStr)
2929
}
3030

31-
// Convert() function that will take Decimal number as int,
31+
// DecimalToBinary() function that will take Decimal number as int,
3232
// and return it's Binary equivalent as string.
33-
func Convert(num int) (string, error) {
33+
func DecimalToBinary(num int) (string, error) {
3434
if num < 0 {
3535
return "", errors.New("integer must have +ve value")
3636
}
@@ -42,5 +42,5 @@ func Convert(num int) (string, error) {
4242
result += strconv.Itoa(num & 1)
4343
num >>= 1
4444
}
45-
return Inverse(result), nil
45+
return Reverse(result), nil
4646
}

conversion/decimaltobinary_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ var decimalTestCases = map[int]string{
2626
100: "1100100",
2727
}
2828

29-
func TestConvert(t *testing.T) {
29+
func TestDecimalToBinary(t *testing.T) {
3030
for input, expected := range decimalTestCases {
31-
out, err := Convert(input)
31+
out, err := DecimalToBinary(input)
3232
if err != nil {
33-
t.Errorf("Convert(%d) returned an error %s", input, err.Error())
33+
t.Errorf("DecimalToBinary(%d) returned an error %s", input, err.Error())
3434
}
3535
if out != expected {
36-
t.Errorf("Convert(%d) = %s; want %s", input, out, expected)
36+
t.Errorf("DecimalToBinary(%d) = %s; want %s", input, out, expected)
3737
}
3838
}
3939
}
4040

41-
func BenchmarkConvert(b *testing.B) {
41+
func BenchmarkDecimalToBinary(b *testing.B) {
4242
b.ReportAllocs()
4343
for i := 0; i < b.N; i++ {
44-
_, _ = Convert(100)
44+
_, _ = DecimalToBinary(100)
4545
}
4646
}

0 commit comments

Comments
 (0)