Repository navigation
Wrap Python's ~ on unsigned and << on fixed-width integers to the C family's width [patch] - #358
Merged
Merged
Conversation
…amily's width [patch] Python integers are unbounded, so ~x on a uint or ulong was negative and x << n on an int, uint, long or ulong never wrapped, disagreeing with every other target. ~ on uint/ulong is now masked to its width, << on the four fixed-width types (and <<=) is masked or wrapped back into the signed range, keyed on the left operand's static type. ~ on signed integers already matched two's complement and is unchanged. Fixes #351 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW6NRQ27ovPhjARcxRW
|
This was referenced Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #351
What changed
Python integers are unbounded, so the generated Python gave different answers from every other target.
~xon auintcame out as-6where the others give4294967290, and1 << 31on anintcame out as2147483648where they give-2147483648. This ports the JavaScript fix from #256 / PR #322 toPythonGenerator, keyed on static type the same way:~onuint/ulongis masked to the width:((~x) & 0xFFFFFFFF)/& 0xFFFFFFFFFFFFFFFF.~onint/longalready matches two's complement, so it is unchanged.<<onuint/ulong, keyed on the left operand, is masked the same way.<<onint/longis wrapped back into the signed range:((((x << n) + 0x80000000) & 0xFFFFFFFF) - 0x80000000), using the 64-bit constants forlong.<<=on those types is written out asx = <wrapped shift>, the same way/=and%=already are.As the issue asked, this covers only the bitwise and shift operators.
>>,&,|and^already give the C family's result on in-range operands, and operands of unknown type are left as they were.Tests
In
WideIntegerBitwiseOperatorTests:Python_FixedWidthBitwiseOperators_MatchCSharpruns the generated module withpython3over the existing JavaScript: bitwise and shift operators on long/uint values give 32-bit-truncated results (flags | 4294967296L → 1) #256 cases plus new ones: unsigned and signed~,int/long/ulongshifts that overflow, and<<=onint/uint. Each result is compared with C#'s. The cases above 2^53 are Python-only, because JavaScript's number conversion rounds them, which JavaScript: bitwise and shift operators on long/uint values give 32-bit-truncated results (flags | 4294967296L → 1) #256 accepted.Python_SignedNotAndOtherOperators_AreNotWrappedchecks that~on anint,>>, and a shift whose left operand has an unknown type are emitted unchanged.With the generator change stashed,
Python_FixedWidthBitwiseOperators_MatchCSharpfails, and it passes with the change. FullCoder.Testsuite: 1553 passed, 0 failed.Generated by Claude Code