IT

OR 열거 형 항목을 제거하는 방법?

lottoking 2020. 6. 6. 21:09
반응형

OR 열거 형 항목을 제거하는 방법?


나는 열거 형이있다 :

public enum Blah
{
    RED = 2,
    BLUE = 4,
    GREEN = 8,
    YELLOW = 16
}

Blah colors = Blah.RED | Blah.BLUE | Blah.YELLOW;

가변 색상에서 파란색을 어떻게 제거합니까?


'BLUE' &~(보완)로 해야합니다 .

보수 연산자는 기본적으로 주어진 데이터 유형에 대한 모든 비트를 반전 시키거나 '플립'합니다. 따라서 AND연산자 ( &)를 일부 값 (값을 'X'라고 함)과 하나 이상의 세트 비트의 보수 (그 비트 Q와 보수를 호출하자 ~Q)와 함께 사용하면 명령문 X & ~Q은 설정된 비트를 지 웁니다. Qfrom X및 결과를 반환합니다.

따라서 BLUE비트 를 제거하거나 지우 려면 다음 명령문을 사용하십시오.

colorsWithoutBlue = colors & ~Blah.BLUE
colors &= ~Blah.BLUE // This one removes the bit from 'colors' itself

다음과 같이 여러 비트를 지울 수 있습니다.

colorsWithoutBlueOrRed = colors & ~(Blah.BLUE | Blah.RED)
colors &= ~(Blah.BLUE | Blah.RED) // This one removes both bits from 'colors' itself

또는 교대로 ...

colorsWithoutBlueOrRed = colors & ~Blah.BLUE & ~Blah.RED
colors &= ~Blah.BLUE & ~Blah.RED // This one removes both bits from 'colors' itself

요약하면 다음과 같습니다.

  • X | Q 비트를 설정 Q
  • X & ~Q 비트를 지 웁니다 Q
  • ~X 모든 비트를 뒤집거나 뒤집습니다. X

다른 답변은 정확하지만 위의 파란색을 구체적으로 제거하려면 다음과 같이 작성하십시오.

colors &= ~Blah.BLUE;


And not 그것...............................

Blah.RED | Blah.YELLOW == 
   (Blah.RED | Blah.BLUE | Blah.YELLOW) & ~Blah.BLUE;

이것은 나처럼 여기에서 우연히 발견 된 다른 사람들에게 유용 할 것이라고 생각했습니다.

값 == 0으로 설정할 수있는 열거 형 값을 처리하는 방법에주의하십시오 (때로는 열거 형에 대해 알 수 없음 또는 유휴 상태를 유지하는 것이 도움이 될 수 있음). 이러한 비트 조작 작업에 의존 할 때 문제가 발생합니다.

또한 2의 다른 거듭 제곱의 조합 인 열거 형 값이있는 경우 (예 :

public enum Colour
{
    None = 0,  // default value
    RED = 2,
    BLUE = 4,
    GREEN = 8,
    YELLOW = 16,
    Orange = 18  // Combined value of RED and YELLOW
}

이러한 경우 다음과 같은 확장 방법이 유용 할 수 있습니다.

public static Colour UnSet(this Colour states, Colour state)
{
    if ((int)states == 0)
        return states;

    if (states == state)
        return Colour.None;

    return states & ~state;
}

또한 결합 된 값을 처리하는 동등한 IsSet 메소드도 있습니다 (약간 해키 방식이지만)

public static bool IsSet(this Colour states, Colour state)
{
    // By default if not OR'd
    if (states == state)
        return true;

    // Combined: One or more bits need to be set
    if( state == Colour.Orange )
        return 0 != (int)(states & state);

    // Non-combined: all bits need to be set
    return (states & state) == state;
}

xor (^)는 어떻습니까?

제거하려는 플래그가 있으면 작동합니다. 그렇지 않은 경우 &를 사용해야합니다.

public enum Colour
{
    None = 0,  // default value
    RED = 2,
    BLUE = 4,
    GREEN = 8,
    YELLOW = 16,
    Orange = 18  // Combined value of RED and YELLOW
}

colors = (colors ^ Colour.RED) & colors;

You can use this:

colors &= ~Blah.RED; 

To simplify the flag enum and make it may be better to read by avoiding multiples, we can use bit shifting. (From a good article Ending the Great Debate on Enum Flags)

[FLAG]
Enum Blah
{
   RED = 1,
   BLUE = 1 << 1,
   GREEN = 1 << 2,
   YELLOW = 1 << 3
}

and also to clear all bits

private static void ClearAllBits()
{
    colors = colors & ~colors;
}

참고URL : https://stackoverflow.com/questions/4778166/how-to-remove-an-item-for-a-ord-enum

반응형