{"record":{"id":"bf00864ac15a364c","repo":"TheAlgorithms/Python","slug":"both-inputs-must-be-positive-integers","errorCode":null,"errorMessage":"both inputs must be positive integers","messagePattern":"both inputs must be positive integers","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/binary_shifts.py","lineNumber":29,"sourceCode":"    Return the shifted binary representation.\n\n    >>> logical_left_shift(0, 1)\n    '0b00'\n    >>> logical_left_shift(1, 1)\n    '0b10'\n    >>> logical_left_shift(1, 5)\n    '0b100000'\n    >>> logical_left_shift(17, 2)\n    '0b1000100'\n    >>> logical_left_shift(1983, 4)\n    '0b111101111110000'\n    >>> logical_left_shift(1, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: both inputs must be positive integers\n    \"\"\"\n    if number < 0 or shift_amount < 0:\n        raise ValueError(\"both inputs must be positive integers\")\n\n    binary_number = str(bin(number))\n    binary_number += \"0\" * shift_amount\n    return binary_number\n\n\ndef logical_right_shift(number: int, shift_amount: int) -> str:\n    \"\"\"\n    Take in positive 2 integers.\n    'number' is the integer to be logically right shifted 'shift_amount' times.\n    i.e. (number >>> shift_amount)\n    Return the shifted binary representation.\n\n    >>> logical_right_shift(0, 1)\n    '0b0'\n    >>> logical_right_shift(1, 1)\n    '0b0'\n    >>> logical_right_shift(1, 5)","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/binary_shifts.py#L11-L47","documentation":"Raised by logical_left_shift when either number or shift_amount is negative. A logical left shift appends zero bits to the binary representation, which is only defined for non-negative operands, so negative inputs are rejected up front.","triggerScenarios":"Calling logical_left_shift(1, -1), logical_left_shift(-4, 2), or any call where either argument is a negative int.","commonSituations":"Computing shift amounts dynamically (e.g., shift = bit_length(a) - bit_length(b)) where the expression can go negative for small inputs; passing user-supplied exponents without range checks.","solutions":["Guard the computed shift amount: only call when shift_amount >= 0, else skip or use logical_right_shift.","Use max(0, shift_amount) if shifting by a negative amount should be a no-op in your logic.","For plain integer arithmetic, use number << shift_amount directly, which raises its own ValueError for negative shifts."],"exampleFix":"# before\nlogical_left_shift(1, bits_a - bits_b)  # ValueError when bits_b > bits_a\n\n# after\nshift = max(0, bits_a - bits_b)\nlogical_left_shift(1, shift)","handlingStrategy":"validation","validationCode":"shift = max(0, shift_amount)\nif number < 0:\n    raise ValueError(\"number must be non-negative\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed shift amounts with max(0, n).","Assert bit-length differences are non-negative before deriving shifts from them.","Remember Python's native << also rejects negative shifts — fix the value, not the call."],"tags":["bit-manipulation","input-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}