{"record":{"id":"7e68445afd0d7f67","repo":"TheAlgorithms/Python","slug":"shift-must-be-non-negative","errorCode":null,"errorMessage":"Shift must be non-negative","messagePattern":"Shift must be non-negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hashes/md5.py","lineNumber":293,"sourceCode":"    >>> left_rotate_32(4294967295, 4)\n    4294967295\n    >>> left_rotate_32(1234, 0)\n    1234\n    >>> left_rotate_32(0, 0)\n    0\n    >>> left_rotate_32(-1, 0)\n    Traceback (most recent call last):\n    ...\n    ValueError: Input must be non-negative\n    >>> left_rotate_32(0, -1)\n    Traceback (most recent call last):\n    ...\n    ValueError: Shift must be non-negative\n    \"\"\"\n    if i < 0:\n        raise ValueError(\"Input must be non-negative\")\n    if shift < 0:\n        raise ValueError(\"Shift must be non-negative\")\n    return ((i << shift) ^ (i >> (32 - shift))) % 2**32\n\n\ndef md5_me(message: bytes) -> bytes:\n    \"\"\"\n    Returns the 32-char MD5 hash of a given message.\n\n    Reference: https://en.wikipedia.org/wiki/MD5#Algorithm\n\n    Arguments:\n        message {[string]} -- [message]\n\n    Returns:\n        32-char MD5 hash string\n\n    >>> md5_me(b\"\")\n    b'd41d8cd98f00b204e9800998ecf8427e'\n    >>> md5_me(b\"The quick brown fox jumps over the lazy dog\")","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/hashes/md5.py#L275-L311","documentation":"Thrown by left_rotate_32() when the shift amount is negative. A negative rotation is undefined for the bit trick used ((i << shift) ^ (i >> (32 - shift))) and Python's arbitrary-precision ints would silently produce wrong results, so shift < 0 is rejected. Note the value guard (error 388) fires first if both are invalid.","triggerScenarios":"Calling left_rotate_32(0, -1), or computing the shift from an expression like (s - t) that can go negative, or indexing a shift table out of range with a negative index.","commonSituations":"Parameterized rotation schedules where the shift is derived at runtime (e.g. variable round constants) and can underflow; typos in shift tables copied from the MD5 spec.","solutions":["Pass shifts in [0, 31] per the MD5 specification; use the fixed shift table verbatim.","Normalize computed shifts: shift % 32.","Validate your shift table length/contents once at module load, not per call."],"exampleFix":"# before\nleft_rotate_32(x, s - t)  # s - t may be negative\n\n# after\nleft_rotate_32(x, (s - t) % 32)","handlingStrategy":"validation","validationCode":"assert 0 <= shift < 32, f\"shift must be in [0, 32), got {shift}\"","typeGuard":"def is_valid_shift(shift: int) -> bool:\n    return isinstance(shift, int) and 0 <= shift < 32","tryCatchPattern":"try:\n    r = left_rotate_32(x, s)\nexcept ValueError as e:\n    if \"Shift\" in str(e):\n        r = left_rotate_32(x, s % 32)\n    else:\n        raise","preventionTips":["Use the MD5 spec's fixed shift tables verbatim.","Normalize computed shifts with % 32.","Validate shift tables once at module import."],"tags":["md5","hash","validation","bit-manipulation","internal-api"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}