why -3==~2 in C#
问题 Unable to understand. Why output is "equal" code: if (-3 == ~2) Console.WriteLine("equal"); else Console.WriteLine("not equal"); output: equal 回答1: Because two's complement bit-arithmetic makes it so Cribbed from the wikipedia page and expanded: Most Significant Bit 6 5 4 3 2 1 0 Value 0 0 0 0 0 0 1 1 3 0 0 0 0 0 0 1 0 2 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 0 -2 1 1 1 1 1 1 0 1 -3 1 1 1 1 1 1 0 0 -4 So you get: 0 0 0 0 0 0 1 0 = 2 1 1 1 1 1 1 0 1 = -3 And as