-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix BitArray.LeftShift to clear dangling high bits #123296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
| ba.LeftShift(1); | ||
| Assert.False(ba.HasAnySet()); | ||
| Assert.Equal(0, ba.PopCount()); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot, please make this test a [Theory] and validate various bit patterns and shift counts rather than only checking one combination. Also, please add similar tests with PopCount for other operations, such as RightShift, and any others that might suffer a similar problem if they inappropriately handle clearing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expanded to [Theory] with MemberData covering various bit patterns and shift counts. Added tests for both LeftShift and RightShift with PopCount and HasAnySet validation. Tests cover edge cases around int32 boundaries (1, 8, 31, 32, 33, 64, 65 bits). See commit 066cb7c.
|
Tagging subscribers to this area: @dotnet/area-system-collections |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request fixes a bug in BitArray.LeftShift where unused high-order bits in the most significant storage element were not being cleared after the shift operation, causing PopCount() to return incorrect values when bits were shifted out of range.
Changes:
- Added a call to
ClearHighExtraBits()in theLeftShiftmethod to clear dangling high bits after shifting - Added regression test
LeftShift_PopCountReturnsCorrectCountto verify the fix
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Collections/BitArray.cs | Added ClearHighExtraBits() call after left shift operation to clear unused high bits |
| src/libraries/System.Collections/tests/BitArray/BitArray_OperatorsTests.cs | Added regression test that verifies PopCount returns 0 after shifting the last bit out of range |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Description
BitArray.LeftShiftwas not clearing unused bits in the most significant storage element after shifting. This causedPopCountto return incorrect values when bits were shifted out of range.Changes
ClearHighExtraBits()after shift operation completes inLeftShiftLeftShiftandRightShiftoperations validatingPopCountandHasAnySetwith various bit patterns and shift countsOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.