C# 字节计算器
C# Byte Calculator
我的问题是,例如,当我将 0x02
添加到 0xFF
时,我得到值 0x101
,这当然是正确的,但是否有可能以某种方式获得值0x01
来自 0xFF + 0x02
。因此,如果值超过 0xFF
,它将自行重置为 0x00
。
Mod 0x100
result = (0x02+0xff)%0x100
如果你想获得一个 byte
让我们 cast 到 byte
(并在可能的 溢出时切换 IntegerOverflowException
在 unchecked
的帮助下关闭):
byte a = 0x02;
byte b = 0xFF;
// unchecked: do not throw exception on overflow
byte result = unchecked((byte) (a + b));
或者(如果您有许多个操作要执行):
byte a = 0x02;
byte b = 0xFF;
unchecked {
byte result1 = (byte) (a + b);
byte result2 = (byte) (2 * a + b);
...
}
我的问题是,例如,当我将 0x02
添加到 0xFF
时,我得到值 0x101
,这当然是正确的,但是否有可能以某种方式获得值0x01
来自 0xFF + 0x02
。因此,如果值超过 0xFF
,它将自行重置为 0x00
。
Mod 0x100
result = (0x02+0xff)%0x100
如果你想获得一个 byte
让我们 cast 到 byte
(并在可能的 溢出时切换 IntegerOverflowException
在 unchecked
的帮助下关闭):
byte a = 0x02;
byte b = 0xFF;
// unchecked: do not throw exception on overflow
byte result = unchecked((byte) (a + b));
或者(如果您有许多个操作要执行):
byte a = 0x02;
byte b = 0xFF;
unchecked {
byte result1 = (byte) (a + b);
byte result2 = (byte) (2 * a + b);
...
}