The signed right shift >> available on earlier versions of .NET preserves the sign bit when shifting right meaning that negative numbers remain negative.
The new unsigned right shift >>> shifts the bits to the right and fills the left bits with zeroes in the same way that << shifts bits to the left and fills the right bits with zeroes.
Code
csharp
int val = -64; // 0b11111111_11111111_11111111_11000000 (-64)
// Binary right shift by 3 bits
int b = val >>> 3; // 0b00011111_11111111_11111111_11111000 (536870904)csharp
int val = -64; // 0b11111111_11111111_11111111_11000000 (-64)
// Cast to uint so >> fills with zeroes, then cast back
int b = (int)((uint)val >> 3); // 0b00011111_11111111_11111111_11111000 (536870904)Notes
- Positive values shifted with
>>>have the same result as if shifted by>> - Also in C# 11, the right hand operand of a user-defined shift operator no longer needs to be an
int; the built-in shift operators still require one