Skip to content

Commit 871f312

Browse files
author
Cam Soper
committed
issue 43303
1 parent ccff70b commit 871f312

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

docs/core/compatibility/10.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ If you're migrating an app to .NET 10, the breaking changes listed here might af
2222
| [API obsoletions with non-default diagnostic IDs](core-libraries/10.0/obsolete-apis.md) | Source incompatible | Preview 1 |
2323
| [ActivitySource.CreateActivity and ActivitySource.StartActivity behavior change](core-libraries/10.0/activity-sampling.md) | Behavioral change | Preview 1 |
2424
| [C# 14 overload resolution with span parameters](core-libraries/10.0/csharp-overload-resolution.md) | Behavioral change | Preview 1 |
25+
| [Consistent shift behavior in generic math](core-libraries/10.0/generic-math.md) | Behavioral change | Preview 1 |
2526
| [LDAP DirectoryControl parsing is now more stringent](core-libraries/10.0/ldap-directorycontrol-parsing.md) | Behavioral change | Preview 1 |
2627

2728
## Globalization
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: "Breaking change: Consistent shift behavior in generic math"
3+
description: Learn about the .NET 10 breaking change in core .NET libraries where shift operations in generic math now have consistent behavior.
4+
ms.date: 01/30/2025
5+
---
6+
# Consistent shift behavior in generic math
7+
8+
This document explains the breaking change in .NET 10 where shift operations in generic math now have consistent behavior across all built-in integer types.
9+
10+
## Previous behavior
11+
12+
The behavior when utilizing generic math to perform a shift on a `T` could differ based on the type. In some cases, it would appropriately mask the shift amount by `sizeof(T) - 1` and in other cases, it would do no masking. This meant that "overshifting" (such as shifting a `byte` by 8) could result in different answers than expected.
13+
14+
## New behavior
15+
16+
The implementations were updated to mask the shift amount, as appropriate, to ensure consistent behavior across all built-in integer types and with the behavior documented by the <xref:System.Numerics.IShiftOperators`3?displayProperty=nameWithType> interface.
17+
18+
## Version introduced
19+
20+
.NET 10 Preview 1
21+
22+
## Type of breaking change
23+
24+
This change is a [behavioral change](../../categories.md#behavioral-change).
25+
26+
## Reason for change
27+
28+
The behavior differed from the designed behavior due to a difference in how masking works for small integer types in C#.
29+
30+
## Recommended action
31+
32+
Update any code that relies on the previous inconsistent behavior to ensure it works with the new consistent behavior.
33+
34+
## Affected APIs
35+
36+
- `operator <<`
37+
- `operator >>`
38+
- `operator >>>` for `byte`, `char`, `sbyte`, `short`, and `ushort` when used via `Generic Math`, which requires a `T` constrained to `where T : IShiftOperators<T, int, T>` or a similar interface.

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ items:
1616
href: core-libraries/10.0/activity-sampling.md
1717
- name: C# 14 overload resolution with span parameters
1818
href: core-libraries/10.0/csharp-overload-resolution.md
19+
- name: Consistent shift behavior in generic math
20+
href: core-libraries/10.0/generic-math.md
1921
- name: LDAP DirectoryControl parsing is now more stringent
2022
href: core-libraries/10.0/ldap-directorycontrol-parsing.md
2123
- name: Globalization
@@ -1330,6 +1332,8 @@ items:
13301332
href: core-libraries/10.0/activity-sampling.md
13311333
- name: C# 14 overload resolution with span parameters
13321334
href: core-libraries/10.0/csharp-overload-resolution.md
1335+
- name: Consistent shift behavior in generic math
1336+
href: core-libraries/10.0/generic-math.md
13331337
- name: LDAP DirectoryControl parsing is now more stringent
13341338
href: core-libraries/10.0/ldap-directorycontrol-parsing.md
13351339
- name: .NET 9

0 commit comments

Comments
 (0)