Bit manipulation 计数大于30的位移位

Bit manipulation 计数大于30的位移位,bit-manipulation,bit-shift,Bit Manipulation,Bit Shift,我对位移位运算符(在c#中)感到困惑 您能否解释一下为什么下面的“a”值返回“1”而不是“4294967296”: ulong a=1将1视为int,向左移动,然后将整个结果(即int)分配给ulonga 试试这个: ulong a = 1 << 32; Console.WriteLine(a); a = (ulong)1 << 32; Console.WriteLine(a); ulong a=1 long a = 1 <<

我对位移位运算符(在c#中)感到困惑

您能否解释一下为什么下面的“a”值返回“1”而不是“4294967296”:


ulong a=1将
1
视为
int
,向左移动,然后将整个结果(即int)分配给
ulong
a

试试这个:

    ulong a = 1 << 32;
    Console.WriteLine(a);
    a = (ulong)1 << 32;
    Console.WriteLine(a);
ulong a=1
long a = 1 << 31; // I want the value 2147483648 not the value -2147483648
long b = 1 << 32; // I want the value 4294967296 not the value 1
    ulong a = 1 << 32;
    Console.WriteLine(a);
    a = (ulong)1 << 32;
    Console.WriteLine(a);