针对int 到 ushort的转换
[TestMethod]
public void TestLowUShortBound()
{
for (int j = 1; j <= 10; j++)
{
ushort res = (ushort)(5 - j);
Console.WriteLine(res);
}
/*
4 0000 0000 0000 0100
3 0000 0000 0000 0011
2 0000 0000 0000 0010
1 0000 0000 0000 0001
0 0000 0000 0000 0000
65535 1111 1111 1111 1111 => -1的2进制=> 原码 0001 0000 0000 0000 0001
65534 1111 1111 1111 1110 反码 1110 1111 1111 1111 1110
65533 1111 1111 1111 1101 补码 1110 1111 1111 1111 1111
65532 1111 1111 1111 1100 只保留2个字节则为 1111 1111 1111 1111, 负数在计算机中使用补码表示,强转为ushort即为ushort的最大值
65531 1111 1111 1111 1011
*/
}
[TestMethod]
public void TestHighUShortBound()
{
int ushortVal = 65535 - 5;
for (int j = 1; j <= 10; j++)
{
ushort res = (ushort)(ushortVal + j);
Console.WriteLine(res);
}
/*
65531
65532
65533
65534
65535
0 65536 => 0000 1111 1111 1111 1111 + 1
1 0001 0000 0000 0000 0000
2 只保留2个字节则为 0000 0000 0000 0000, 强转为ushort即为ushort的最最小值
3
4
*/
}