如何做到这一点:
{
byte x = 0;
x |= 2;
x &= 0xFF & ~2;
}
解释: 这里有两个问题。 第一,固定运营商:
C# Spec 4.1.5:
The integral-type unary and binary operators always operate with signed
32-bit precision, unsigned 32-bit
precision, signed 64-bit precision, or
unsigned 64-bit precision:
For the unary + and ~ operators, the operand is converted to type T,
where T is the first of int, uint,
long, and ulong that can fully
represent all possible values of the
operand. The operation is then
performed using the precision of type
T, and the type of the result is T.
一旦您申请独立经营人,结果总是在最小的某个类型。 从那以后,你想要暗中地转换成秘密。
第二,。
A constant expression of type int can
be converted to sbyte, byte, short,
ushort, uint, or ulong, provided the
value of the constant expression is
within the range of the destination
type.
因此,大约2,总是暗中。 不能不言而喻地加以转换,因为它不属于范围。 如果你限制在幅度之内,你可以暗中转换。