先上结论
右移:
1) 有符号数:左侧会使用符号位补齐
2) 无符号数:左侧补0
左移:符号位也会被左移,右侧补0
取反:符号位也会被取反
测试代码
//有符号数 static void Print(int a) { Console.WriteLine($"{Convert.ToString(a, 2).PadLeft(32, '0')}, {a}"); int b = a >> 1; Console.WriteLine($"{Convert.ToString(b, 2).PadLeft(32, '0')} 右移, {b}"); int c = a << 1; Console.WriteLine($"{Convert.ToString(c, 2).PadLeft(32, '0')} 左移, {c}"); int d = ~a; Console.WriteLine($"{Convert.ToString(d, 2).PadLeft(32, '0')} 取反, {d}"); Console.WriteLine(); } //无符号数 static void Print2(uint a) { Console.WriteLine($"{Convert.ToString(a, 2).PadLeft(32, '0')}, {a}"); uint b = a >> 1; Console.WriteLine($"{Convert.ToString(b, 2).PadLeft(32, '0')} 右移, {b}"); uint c = a << 1; Console.WriteLine($"{Convert.ToString(c, 2).PadLeft(32, '0')} 左移, {c}"); uint d = ~a; Console.WriteLine($"{Convert.ToString(d, 2).PadLeft(32, '0')} 取反, {d}"); Console.WriteLine(); }
运行后:
static void Main(string[] args) { uint a = 0x80000000; Print((int)a); Print2(a); }
运行结果:
标签:位操作,Convert,Console,符号,int,PadLeft,WriteLine,影响 From: https://www.cnblogs.com/sailJs/p/16905943.html