{"record":{"id":"9e97e4c5c326c374","repo":"TheAlgorithms/Go","slug":"binary-number-must-be-in-range-0-to-2-31-1","errorCode":null,"errorMessage":"binary number must be in range 0 to 2^(31-1)","messagePattern":"binary number must be in range 0 to 2\\^\\(31-1\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/binarytodecimal.go","lineNumber":32,"sourceCode":"\npackage conversion\n\n// Importing necessary package.\nimport (\n\t\"errors\"\n\t\"regexp\"\n)\n\nvar isValid = regexp.MustCompile(\"^[0-1]{1,}$\").MatchString\n\n// BinaryToDecimal() function that will take Binary number as string,\n// and return its Decimal equivalent as an integer.\nfunc BinaryToDecimal(binary string) (int, error) {\n\tif !isValid(binary) {\n\t\treturn -1, errors.New(\"not a valid binary string\")\n\t}\n\tif len(binary) > 32 {\n\t\treturn -1, errors.New(\"binary number must be in range 0 to 2^(31-1)\")\n\t}\n\tvar result, base int = 0, 1\n\tfor i := len(binary) - 1; i >= 0; i-- {\n\t\tif binary[i] == '1' {\n\t\t\tresult += base\n\t\t}\n\t\tbase *= 2\n\t}\n\treturn result, nil\n}\n","sourceCodeStart":14,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/binarytodecimal.go#L14-L43","documentation":"BinaryToDecimal limits input to at most 32 characters so the accumulated value fits in a 32-bit signed integer range (0 to 2^31-1). Longer strings would overflow the int result on 32-bit platforms, so the function rejects them up front. The error is created ad-hoc via errors.New.","triggerScenarios":"BinaryToDecimal called with a binary string longer than 32 characters, e.g. a 64-bit binary literal like \"10000000000000000000000000000000001\".","commonSituations":"Parsing 64-bit binary values (uint64 ranges) or binary hashes/MACs rendered as long bit strings; code assuming the function handles arbitrary-width binaries.","solutions":["Check len(binary) <= 32 before calling, and reject or chunk longer inputs.","For larger values, use strconv.ParseUint(binary, 2, 64) instead of this library.","Switch to math/big: new(big.Int).SetString(s, 2) for arbitrary precision."],"exampleFix":"// before\nval, err := conversion.BinaryToDecimal(sixtyFourBits) // error: must be in range 0 to 2^(31-1)\n// after\nu, err := strconv.ParseUint(sixtyFourBits, 2, 64) // handles up to 64 bits","handlingStrategy":"validation","validationCode":"func fitsIntBinary(s string) bool { return len(s) <= 32 }","typeGuard":null,"tryCatchPattern":"val, err := conversion.BinaryToDecimal(input)\nif err != nil {\n    if err.Error() == \"binary number must be in range 0 to 2^(31-1)\" {\n        return fmt.Errorf(\"value too large for this converter: %w\", err)\n    }\n    return err\n}","preventionTips":["Check length <= 32 before converting.","Use strconv.ParseUint(s, 2, 64) or math/big for wider values.","Treat this converter as 31-bit-range only."],"tags":["conversion","binary","overflow","go"],"backgroundTag":"integer-overflow-range","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}