为什么 [1111 1111] 在 Integer 中表示为 -1 而不是 255?

Why [1111 1111] represents as -1 but not 255 in Integer?

我根据规则进行了数字符号替换:逐位取反,然后加 1,但我使用的是整数数据类型,而不是 sbyte。编译器如何理解我正在更改符号,

不返回值 255?

        int operand1 = 0, operand2 = 0;
        int result;

        operand1 = 0x01;                            // [0000 0001]
        result = ~operand1;                         // [1111 1110]
        result++;                                   // [1111 1111]

        Console.WriteLine(" ~ {0} + 1 = {1} ", operand1, result);

输出: " ~ 1 + 1 = -1 "

有符号和无符号整数。带符号的整数可以包含负值,因此范围的 "upper" 部分从 (0- (int.max / 2)) 开始计数。

查看这篇文章: https://en.wikipedia.org/wiki/Two%27s_complement

如果您使用 Unsigned int,它的行为就像我想的那样。

在有符号整数中,最高位决定它是否为负值。