Discussion:
Unexpected behavior shifting uint 32 bits
(too old to reply)
nickdu
2009-09-22 13:15:01 UTC
Permalink
While I had a bug in my code where a variable was typed as a uint as opposed
to a ulong, I was surprised to find that shifting a uint right 32 bits did
not produce a zero. Instead it appears the uint was not modified. Is there
a reason for this?

The below sample:

using System;

public class Application
{
public static void Main()
{
uint value = 4;

value = value >> 32;
Console.WriteLine("value = {0}", value);
}
}

generates:

value = 4
--
Thanks,
Nick

***@community.nospam
remove "nospam" change community. to msn.com
Stephen Myers <""StephenMyers\"@">
2009-09-22 17:20:49 UTC
Permalink
Post by nickdu
While I had a bug in my code where a variable was typed as a uint as opposed
to a ulong, I was surprised to find that shifting a uint right 32 bits did
not produce a zero. Instead it appears the uint was not modified. Is there
a reason for this?
using System;
public class Application
{
public static void Main()
{
uint value = 4;
value = value >> 32;
Console.WriteLine("value = {0}", value);
}
}
value = 4
From VS 2008 Documentation
Post by nickdu
Operator (C# Reference)
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).


Steve

Loading...