Sha256: 75e628cd62382601a0ae42b25d70933f18d4e7c72f325b7e0222cd5fccb3856b

Contents?: true

Size: 485 Bytes

Versions: 122

Compression:

Stored size: 485 Bytes

Contents

package binary

import (
	"fmt"
)

// ParseBinary converts a binary string to an integer value
func ParseBinary(bin string) (int, error) {
	val := 0
	for _, digit := range bin {
		switch digit {
		case '1':
			// multiply val by 2 and add 1
			val = (val << 1) + 1
		case '0':
			// multiply val by 2 and add 0
			val = (val << 1) + 0
		default:
			// if the character was not 1 or 0, it must be invalid
			return 0, fmt.Errorf("unexpected rune '%c'", digit)
		}
	}
	return val, nil
}

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/binary/example.go
trackler-2.2.1.179 tracks/go/exercises/binary/example.go
trackler-2.2.1.178 tracks/go/exercises/binary/example.go
trackler-2.2.1.177 tracks/go/exercises/binary/example.go
trackler-2.2.1.176 tracks/go/exercises/binary/example.go
trackler-2.2.1.175 tracks/go/exercises/binary/example.go
trackler-2.2.1.174 tracks/go/exercises/binary/example.go
trackler-2.2.1.173 tracks/go/exercises/binary/example.go
trackler-2.2.1.172 tracks/go/exercises/binary/example.go
trackler-2.2.1.171 tracks/go/exercises/binary/example.go
trackler-2.2.1.170 tracks/go/exercises/binary/example.go
trackler-2.2.1.169 tracks/go/exercises/binary/example.go
trackler-2.2.1.167 tracks/go/exercises/binary/example.go
trackler-2.2.1.166 tracks/go/exercises/binary/example.go
trackler-2.2.1.165 tracks/go/exercises/binary/example.go
trackler-2.2.1.164 tracks/go/exercises/binary/example.go
trackler-2.2.1.163 tracks/go/exercises/binary/example.go
trackler-2.2.1.162 tracks/go/exercises/binary/example.go
trackler-2.2.1.161 tracks/go/exercises/binary/example.go
trackler-2.2.1.160 tracks/go/exercises/binary/example.go