Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unexpected behavior shifting uint 32 bits

2 views
Skip to first unread message

nickdu

unread,
Sep 22, 2009, 9:15:01 AM9/22/09
to
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

nickno...@community.nospam
remove "nospam" change community. to msn.com

Stephen Myers

unread,
Sep 22, 2009, 1:20:49 PM9/22/09
to
nickdu wrote:
> 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
>
From VS 2008 Documentation

>> 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

0 new messages