为什么不是 ~ of 6 = 1 因为它翻转位?

Why isn't ~ of 6 = 1 since it flips the bits?

请参阅下面的示例。如果我们考虑字节类型中的所有位,我希望它是 1 或 249。

我阅读了资料,但我不明白为什么我无法理解它。

byte num1 = 6;
byte num2 = 12;
Assert.IsTrue(~num1 == 1);
Assert.IsTrue(~num2 == 3);

~ 运算符将类型转换为 int。如果你想要 byte - 使用 cast:

byte num1 = 6;
byte num2 = 12;
Console.WriteLine((byte)~num1);
Console.WriteLine((byte)~num2);