c# uint right bit-shift unexpected result

1

I am confused why does this produce 0xffffffff in C#, I was expecting it to produce 0x0. The type of the expression is uint.

Console.WriteLine(0xffffffff >> 32);
c#
bitwise-operators
bit-shift
asked on Stack Overflow Dec 7, 2018 by user3700562

1 Answer

3

As per the documentation:

If the first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of the second operand (second operand & 0x1f).

The second operand is 32. 32 & 0x1f is 0. Thus >> 32 is equivalent to 'shift this by 0 bits' therefore 'do nothing at all'.

answered on Stack Overflow Dec 7, 2018 by mjwills

User contributions licensed under CC BY-SA 3.0