假设我有一个枚举Direction,指定基数(和心轴间)方向:
public enum Direction
{
None = 0,
Up = 1 << 1,
Down = 1 << 2,
Left = 1 << 3,
Right = 1 << 4,
UpLeft = Up | Left,
UpRight = Up | Right,
DownLeft = Down | Left,
DownRight = Down | Right
}
Run Code Online (Sandbox Code Playgroud)
在不引入冲突方向(向上和向下)的情况下,为了反转任意方向,我可以执行的最短逐位运算是什么?
我知道我可以使用三级语句来有效地处理这个问题
public Direction Inverse (Direction d) {
// Inverse of Direction.None should still be none
if (d == Direction.None) { return d; }
Direction dInvert = (d.HasFlag(Direction. Up)) …Run Code Online (Sandbox Code Playgroud)