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