Sha256: b1f0be9d13da0fb3d785968bce0c0b2b7013d140f0bef20c0e3312ecdd93d437

Contents?: true

Size: 508 Bytes

Versions: 274

Compression:

Stored size: 508 Bytes

Contents

package binary

import (
	"fmt"
)

const testVersion = 2

// 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

274 entries across 274 versions & 1 rubygems

Version Path
trackler-2.0.8.13 tracks/go/exercises/binary/example.go
trackler-2.0.8.12 tracks/go/exercises/binary/example.go
trackler-2.0.8.11 tracks/go/exercises/binary/example.go
trackler-2.0.8.10 tracks/go/exercises/binary/example.go
trackler-2.0.8.9 tracks/go/exercises/binary/example.go
trackler-2.0.8.8 tracks/go/exercises/binary/example.go
trackler-2.0.8.7 tracks/go/exercises/binary/example.go
trackler-2.0.8.6 tracks/go/exercises/binary/example.go
trackler-2.0.8.5 tracks/go/exercises/binary/example.go
trackler-2.0.8.4 tracks/go/exercises/binary/example.go
trackler-2.0.8.3 tracks/go/exercises/binary/example.go
trackler-2.0.8.2 tracks/go/exercises/binary/example.go
trackler-2.0.8.1 tracks/go/exercises/binary/example.go
trackler-2.0.7.0 tracks/go/exercises/binary/example.go
trackler-2.0.6.44 tracks/go/exercises/binary/example.go
trackler-2.0.6.43 tracks/go/exercises/binary/example.go
trackler-2.0.6.42 tracks/go/exercises/binary/example.go
trackler-2.0.6.41 tracks/go/exercises/binary/example.go
trackler-2.0.6.40 tracks/go/exercises/binary/example.go
trackler-2.0.6.39 tracks/go/exercises/binary/example.go