목차
- 연산자 정의
- 산술연산자
- 할당연산자
- 비교연산자
- 논리연산자
- 비트연산자
- 연산자 우선순위
연산자 정의
1. 연산자 : 변수와 값에 대한 작업 수행.
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 3 + 4;
Console.WriteLine(x); // 7
}
}
}
2. (값과 값, 변수와 값, 변수와 변수) 연산 가능.
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int sum1 = 2 + 3; // 5
int sum2 = sum1 + 5; // 10
int sum3 = sum2 + sum2; // 20
Console.WriteLine(sum1);
Console.WriteLine(sum2);
Console.WriteLine(sum3);
}
}
}
산술연산자
숫자 연산. (= 산수 계산)
[ + : 덧셈]
[ - : 뺄셈]
[ * : 곱셈]
[ / : 나눗셈]
[ % : 나머지] ★
[ ++ : 1씩 증가]
[ -- : 1씩 감소]
[ + : 덧셈]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x + y); // 8
}
}
}
[ - : 뺄셈]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x - y); // 2
}
}
}
[ * : 곱셈]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x * y); // 15
}
}
}
[ / : 나눗셈]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 12;
int y = 3;
Console.WriteLine(x / y); // 4
}
}
}
[ % : 나머지] ★
※ 나눈 후, 나머지를 반환.
※ 배수 (최소공배수, 최대공약수) 구할 때 사용.
※ x%y == 0 : x는 y의 배수 (예) 18%3 == 0
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x % y); // 2
}
}
}
[ ++ : 1씩 증가]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 3;
x++;
Console.WriteLine(x); // 4
}
}
}
[ -- : 1씩 감소]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 3;
x--;
Console.WriteLine(x); // 2
}
}
}
PS.
출력 구문 안에서 ++, -- 증감연산자가 변수 뒤에 붙은 경우,
출력 구문 실행 후 값이 증감됨.
(예제)
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 1;
Console.WriteLine(x++); // 1
Console.WriteLine(x); // 2
}
}
}
할당연산자
값을 변수에 할당 (= 배정).
[ = : 할당]
[ += : 덧셈 후 할당]
[ -= : 뺄셈 후 할당]
[ *= : 곱셈 후 할당]
[ /= : 나눗셈 후 할당]
[ %= : 나눗셈 후, 나머지 할당] ★
[ &= : AND 비트 연산 후 할당]
[ |= : OR 비트 연산 후 할당]
[ ^= : XOR 비트 연산 후 할당]
[ >>= : Signed right shift 비트 연산 후 할당]
[ <<= : Zero fill left shift 비트 연산 후 할당]
[ = : 할당]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 3;
Console.WriteLine(x); // 3
}
}
}
[ += : 덧셈 후 할당]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x += 3; // x = x + 3
Console.WriteLine(x); // 8
}
}
}
[ -= : 뺄셈 후 할당]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x -= 3; // x = x - 3
Console.WriteLine(x); // 2
}
}
}
[ *= : 곱셈 후 할당]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x *= 3; // x = x * 3
Console.WriteLine(x); // 15
}
}
}
[ /= : 나눗셈 후 할당]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
double x = 5;
x /= 3; // x = x / 3
Console.WriteLine(x); // 1.66666666666667
}
}
}
PS. 변수 x가 int 자료형 경우 결과값 : 1
[ %= : 나눗셈 후, 나머지 할당] ★
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x %= 3; // x = x % 3
Console.WriteLine(x); // 2
}
}
}
[ &= : AND 비트 연산 후 할당]
※ 두 비트 비교해 모두 1이면 각 비트를 1로 설정 후 할당.
※ 5의 이진수 : 00000101
※ 3의 이진수 : 00000011
※ & 연산 시 : 00000001 (※ 이걸 10진수로 변환 시 1)
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x &= 3; // x = x & 3
Console.WriteLine(x); // 1
}
}
}
[ |= : OR 비트 연산 후 할당]
※ 두 비트 중 하나가 1이면 각 비트를 1로 설정 후 할당.
※ 5의 이진수 : 00000101
※ 3의 이진수 : 00000011
※ |= 연산 시 : 00000111 (※ 이걸 10진수로 변환 시 7)
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x |= 3; // x = x | 3
Console.WriteLine(x); // 7
}
}
}
[ ^= : XOR 비트 연산 후 할당]
※ 두 비트 중 하나만 1이면 각 비트를 1로 설정 후 할당.
※ 5의 이진수 : 00000101
※ 3의 이진수 : 00000011
※ ^= 연산 시 : 00000110 (※ 이걸 10진수로 변환 시 6)
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x ^= 3; // x = x ^ 3
Console.WriteLine(x); // 6
}
}
}
[ >>= : Signed right shift 비트 연산 후 할당]
※ 왼쪽 끝 비트 복사해 왼쪽에 밀어넣어 오른쪽으로 해당 숫자 만큼 이동 후, 오른쪽 끝 비트 제거.
※ 5의 이진수 00000101을 오른족으로 3칸 이동 시, 00000000 됨.
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x >>= 3; // x = x >> 3
Console.WriteLine(x); // 0
}
}
}
[ <<= : Zero fill left shift 비트 연산 후 할당]
※ 오른쪽 끝에 0 밀어넣어 왼쪽으로 해당 숫자만큼 이동 후, 왼쪽 끝 비트 제거.
※ 5의 이진수 00000101을 왼쪽으로 3칸 이동 시, 00101000 됨.
※ 결국, 2의 5승 (= 32) + 2의 3승 (= 8) 더하면 40 됨.
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x <<= 3; // x = x << 3
Console.WriteLine(x); // 40
}
}
}
비교연산자
두 값을 비교. (= 두 값의 관계)
[ == : 같음]
[ != : 같지 않음]
[ > : 더 큼]
[ < : 더 작음]
[ >= : 더 크거나 같음]
[ <= : 더 작거나 같음]
[ == : 같음]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x == y); // False
}
}
}
[ != : 같지 않음]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x != y); // True
}
}
}
[ > : 더 큼]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x > y); // True
}
}
}
[ < : 더 작음]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x < y); // False
}
}
}
[ >= : 더 크거나 같음]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x >= y); // True
}
}
}
[ <= : 더 작거나 같음]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x <= y); // False
}
}
}
논리연산자
조건 구문 결합. (※ 소문자로 기재.)
[ && : 그리고]
[ || : 또는]
[ ! : 결과의 역 반환]
[ && : 그리고]
※ 모두 true여야 true 반환.
※ && 대신 소문자 and 표기 시 에러 남. (cf. Java도 에러 남.)
※ 3 < x < 10 처럼 표현하면 에러 발생. 아래치럼 나눠서 표현.
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
Console.WriteLine(x > 3 && x < 10); // True
}
}
}
[ || : 또는]
※ 하나라도 true이면 true 반환.
※ || 대신 소문자 or 표기 시 에러 남. (cf. Java도 에러 남.)
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
Console.WriteLine(x > 3 || x < 4); // True
}
}
}
[ ! : 결과의 역 반환]
※ true이면 False 반환.
※ false이면 True 반환.
※ ! 대신 소문자 not 표기 시 에러 발생. (cf. Java도 에러 남.)
※ &&・|| 연산자보다 우선.
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
int x = 5;
Console.WriteLine(!(x > 3 && x < 10)); // False
}
}
}
비트연산자
있지만, 거의 쓸 일 없어 패스. ㅡㅡ;
공부하실 분은 MS C# 강의 이용.
연산자 우선순위
(산술 > 비교 > ! 논리 > && 및 || 논리)연산자 순.
PS. 산술연산자 경우
1순위 : * (곱셈) , / (나눗셈) , // (몫) , % (나머지)
2순위 : + (덧셈), - (뺄셈)
[예제]
using System;
namespace Homzzang
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(3*4 > 3+4 && !(3*4>10)); // False
}
}
}
최신댓글