{"record":{"id":"c7ae2e9f44b6bc10","repo":"donnemartin/interactive-coding-challenges","slug":"invalid-value","errorCode":null,"errorMessage":"Invalid value","messagePattern":"Invalid value","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"bit_manipulation/bit/bit_solution.ipynb","lineNumber":239,"sourceCode":"    \"        self.number &= mask\\n\",\n    \"        return self.number\\n\",\n    \"\\n\",\n    \"    @validate_index\\n\",\n    \"    def clear_bits_msb_to_index(self, index):\\n\",\n    \"        mask = (1 << index) - 1\\n\",\n    \"        self.number &= mask\\n\",\n    \"        return self.number\\n\",\n    \"\\n\",\n    \"    @validate_index\\n\",\n    \"    def clear_bits_index_to_lsb(self, index):\\n\",\n    \"        mask = ~((1 << index + 1) - 1)\\n\",\n    \"        self.number &= mask\\n\",\n    \"        return self.number\\n\",\n    \"\\n\",\n    \"    @validate_index\\n\",\n    \"    def update_bit(self, index, value):\\n\",\n    \"        if value is None or value not in (0, 1):\\n\",\n    \"            raise Exception('Invalid value')\\n\",\n    \"        if self.get_bit(index) == value:\\n\",\n    \"            return self.number\\n\",\n    \"        if value:\\n\",\n    \"            self.set_bit(index)\\n\",\n    \"        else:\\n\",\n    \"            self.clear_bit(index)\\n\",\n    \"        return self.number\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/bit/bit_solution.ipynb#L221-L257","documentation":"Bit.update_bit raises a generic Exception('Invalid value') when the value parameter is None or not exactly 0 or 1. Bit fields can only be set to a single bit, so any other value is rejected before set_bit/clear_bit is called.","triggerScenarios":"bit.update_bit(3, 2), bit.update_bit(0, True) (bool is not in (0,1) check? True == 1 so it passes), bit.update_bit(1, None), or passing an int parsed from unvalidated string input like int('5').","commonSituations":"Passing user-provided or config-driven flag values without normalizing booleans, or mapping textual 'true'/'false' to something other than 0/1.","solutions":["Convert to int explicitly: update_bit(index, int(bool(value)))","Validate/normalize input to 0 or 1 before the call","Restructure to use set_bit/clear_bit directly based on a boolean"],"exampleFix":"# before\nbit.update_bit(index, value)  # value may be 2/None\n\n# after\nbit.update_bit(index, 1 if value else 0)","handlingStrategy":"validation","validationCode":"if value not in (0, 1):\n    raise ValueError(f'value must be 0 or 1, got {value!r}')\nbit.update_bit(index, value)","typeGuard":"def is_bit_value(v) -> bool:\n    return v in (0, 1)","tryCatchPattern":"try:\n    bit.update_bit(index, value)\nexcept Exception as e:\n    if str(e) == 'Invalid value':\n        value = int(bool(value))\n        bit.update_bit(index, value)\n    else:\n        raise","preventionTips":["Normalize booleans to 0/1 before bit APIs","Validate parsed numeric input against the (0,1) domain","Prefer set_bit/clear_bit when driving from a boolean"],"tags":["python","bit-manipulation","value-validation","generic-exception"],"backgroundTag":"invalid-argument-value","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}