Boolean Logical Operator : 부울 논리 연산자 - AND, OR, NOT, XOR
논리 부울 연산자는 부울 피연산자를 사용하여 논리 연산을 수행합니다.
연산자는 단항 논리 부정(!), 이진 논리 AND(), OR(& |) 및 배타적 OR(^) 및 이진 조건부 논리 AND(&&) 및 OR(||)을 포함합니다.
Logical Negative Operator : 논리 부정 연산자 : !
단항 접두사 ! 연산자는 해당 피연산자의 논리 부정을 계산합니다.
즉, 피연산자가 false로 평가되는 경우 true를 생성하고, 피연산자가 true로 평가되는 경우 false를 생성합니다.
bool passed = false;
Console.WriteLine(!passed); // output: True
Console.WriteLine(!true); // output: False
Logical AND Operator : 논리 AND 연산자 : &
& 연산자는 해당 피연산자의 논리 AND를 컴퓨팅합니다.
x 및 y가 모두 true로 평가되면 x & y의 결과는 true 입니다. 그렇지 않으면 결과는 false입니다.
왼쪽 피연산작가 false로 평가되더라도 & 연산자는 두 피연산자를 평가하여 오른쪽 피연산자의 값에 관계없이 false이어야 합니다.
다음 예제에서 & 연산자의 오른쪽 피연산자는 왼쪽 피연산자의 값과 관계없이 수행되는 메서드 호출입니다.
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}
bool a = false & SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// False
bool b = true & SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
조건부 논리 AND 연산 &&자는 피연산자의 논리 AND도 계산하지만 왼쪽 피연산자가 false로 평가되는 경우
오른쪽 피연산자를 평가하지 않습니다.
정수 형식 피연산자의 경우 & 연산자는 해당 피연산자의 비트 논리 AND를 컴퓨팅합니다.
단항 & 연산자는 address-of 연산자입니다.
Logical Exclusive Operator : 논리 배타적 OR 연산자 : ^
^ 연산자는 해당 피연산자의 논리 XOR이라고도 하는 논리 배타적 OR을 컴퓨팅합니다.
x가 true로 평가되고 y가 false로 평가되거나, x가 false로 평가되고 y가 true로 평가되는 경우 x ^ y의 결과는 true 입니다.
그렇지 않으면 결과는 false 입니다.
즉, bool 피연산자의 경우 연산자는 ^ 같지 않음 연산자 != 와 동일한 결과를 계산합니다.
Console.WriteLine(true ^ true); // output: False
Console.WriteLine(true ^ false); // output: True
Console.WriteLine(false ^ true); // output: True
Console.WriteLine(false ^ false); // output: False
정수 숫자 형식 피연산자의 경우 ^ 연산자는 해당 피연산자의 배타적 비트 논리 OR을 컴퓨팅합니다.
Logical OR Operator : 논리 OR 연산자 : |
| 연산자는 해당 피연산자의 논리 OR을 컴퓨팅합니다.
x 또는 y가 true로 평가되면 x | y 의 결과는 true입니다. 그렇지 않으면 false 입니다.
왼쪽 피연산자가 true로 평가되더라도 |연산자는 두 피연산자를 평가하여
오른쪽 피연산자의 값에 관계없이 true이어야 합니다.
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}
bool a = true | SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// True
bool b = false | SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
Conditional Logical Operator : 조건부 논리 AND 연산자 : &&
"단락(short - circuiting)" 논리 AND 연산자로도 알려진 조건부 논리 AND 연산자 && 는 해당 피연산자의 논리 AND를 컴퓨팅합니다.
x 및 y가 모두 true로 평가되면 x && y의 결과는 true 입니다.
그렇지 않으면 결과는 false입니다.
y가 false로 평가되는 순간 x는 평가되지 않습니다.
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}
bool a = false && SecondOperand();
Console.WriteLine(a);
// Output:
// False
bool b = true && SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
Conditional Logical Operator : 조건부 논리 OR 연산자 : ||
"단락(short - circuiting)" 논리 OR 연산자로도 알려진 조건부 논리 OR 연산자 || 는 해당 피연산자의 논리 OR를 컴퓨팅합니다.
x 또는 y가 true로 평가되면 x || y의 결과는 true 입니다.
그렇지 않으면 결과는 false입니다.
y가 false로 평가되는 순간 x는 평가되지 않습니다.
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}
bool a = true || SecondOperand();
Console.WriteLine(a);
// Output:
// True
bool b = false || SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
Nullable Boolean Logical Operator : Nullable 부울 논리 연산자 : ?
bool? 피연산자의 경우 & 및 | 연산자는 다음과 같이 값이 세 개인 논리를 지원합니다.
& 연산자는 두 피연산자가 모두 true로 평가되는 경우에만 true를 생성합니다.
x 또는 y가 false로 평가되는 경우 다른 피연산자가 null로 평가되더라도 x & y는 false를 생성합니다.
그러지 않으면 x & y의 결과는 null입니다.
| 연산자는 두 피연산자가 모두 false로 평가되는 경우에만 false를 생성합니다.
x 또는 y가 true로 평가되는 경우 다른 피연산자가 null로 평가되더라도 x | y는 true를 생성합니다.
그러지 않으면 x | y 의 결과는 null 입니다.
x | y | x & y | x | y |
true | true | true | true |
true | false | false | true |
true | null | null | true |
false | true | false | true |
false | false | false | false |
false | null | false | null |
null | true | null | true |
null | false | false | null |
null | null | null | null |
해당 연산자의 동작은 Nullable 값 형식을 사용하는 일반 연산자 동작과 다릅니다.
일반적으로 값 형식의 피연산자에 대해 정의된 연산자를 해당 nullable 값 형식의 피연산자에 사용할 수 도 있습니다.
피연산자가 null로 평가되는 경우에도 & 및 | 연산자는 null이 아닌 값을 생성합니다.
bool? test = null;
Display(!test); // output: null
Display(test ^ false); // output: null
Display(test ^ null); // output: null
Display(true ^ null); // output: null
void Display(bool? b) => Console.WriteLine(b is null ? "null" : b.Value.ToString());
조건부 논리 연산자 && 및 || 는 bool? 피연산자를 지원하지 않습니다.
복합 할당
이진 연산자 (op)의 경우 양식의 복합 할당식
bool test = true;
test &= false;
Console.WriteLine(test); // output: False
test |= true;
Console.WriteLine(test); // output: True
test ^= false;
Console.WriteLine(test); // output: True
연산자 우선 순위
다음 목록에서는 논리 연산자를 가장 높은 우선 순위부터 가장 낮은 것으로 정렬합니다.
논리 부정 연산자 !
논리 AND 연산자 &
논리 배타적 OR 연산자 ^
논리 OR 연산자 |
조건부 논리 AND 연산자 &&
조건부 논리 OR 연산자 ||
괄호를 사용하여 연산자 우선 순위에 따라 주어진 평가 순서를 변경합니다.
Console.WriteLine(true | true & false); // output: True
Console.WriteLine((true | true) & false); // output: False
bool Operand(string name, bool value)
{
Console.WriteLine($"Operand {name} is evaluated.");
return value;
}
var byDefaultPrecedence = Operand("A", true) || Operand("B", true) && Operand("C", false);
Console.WriteLine(byDefaultPrecedence);
// Output:
// Operand A is evaluated.
// True
var changedOrder = (Operand("A", true) || Operand("B", true)) && Operand("C", false);
Console.WriteLine(changedOrder);
// Output:
// Operand A is evaluated.
// Operand C is evaluated.
// False
'C#' 카테고리의 다른 글
if문 - switch문 (0) | 2023.01.09 |
---|---|
C# Operator_4 : 비교 연산자 (0) | 2023.01.09 |
C# Operator_3 : 같음 연산자 (0) | 2023.01.05 |
C# Operator_1 : 산술 연산자 (0) | 2023.01.04 |
C# Variable : 변수 (0) | 2023.01.03 |