DotNet · 2022年4月22日

C#位移运算符

<< 和>>

<< 把字节往左移
如1 << 5   表示字节往左移5位,
最多移动位数为当前字节的总长度,超过总长度后,会自动扣减相应位数,又从头开始移
如:
            UInt32 p = UInt32.MaxValue;
            for (int i = 0; i < 33; i++)    //此处多移动了一位,会自动去掉长度32,相当于1
            {
                Console.WriteLine (1 << i);
            }
            Console.ReadLine();

 
应用案例:把整型每一bit的值输出
            int  p = int.MaxValue - 32 ;
            for (int i = 0; i < 32; i++)
            {
                Console.Write(((p & (1 << i)) > 0) ? 1 : 0);
            }
            Console.ReadLine();

最新电影,电视剧,尽在午夜剧场

电影电视剧午夜不寂寞