목차
연산자 정의
산술연산자
할당연산자
비교연산자
논리연산자
비트연산자
연산자 우선순위
연산자 정의
1. 연산자 : 변수와 값에 대한 작업 수행.
#include <iostream>
using namespace std;
int main() {
int x = 3 + 4;
cout << x;
return 0;
}
2. (값과 값, 변수와 값, 변수와 변수) 연산 가능.
#include <iostream>
using namespace std;
int main() {
int sum1 = 2 + 3; // 5
int sum2 = sum1 + 5; // 10
int sum3 = sum2 + sum2; // 20
cout << sum1 << "\n";
cout << sum2 << "\n";
cout << sum3;
return 0;
}
산술연산자
숫자 연산. (= 산수 계산).
[ + : 덧셈]
[ - : 뺄셈]
[ * : 곱셈]
[ / : 나눗셈]
[ % : 나머지] ★
[ ++ : 1씩 증가]
[ -- : 1씩 감소]
[ + : 덧셈]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << x + y; // 8
return 0;
}
[ - : 뺄셈]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << x - y; // 2
return 0;
}
[ * : 곱셈]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << x * y; // 15
return 0;
}
[ / : 나눗셈]
#include <iostream>
using namespace std;
int main() {
int x = 12;
int y = 3;
cout << x / y; // 4
return 0;
}
[ % : 나머지] ★
※ 나눈 후, 나머지를 반환.
※ 배수 (최소공배수, 최대공약수) 구할 때 사용.
※ x%y == 0 : x는 y의 배수 (예) 18%3 == 0
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << x % y; // 2
return 0;
}
[ ++ : 1씩 증가]
#include <iostream>
using namespace std;
int main() {
int x = 3;
++x;
cout << x; // 4
return 0;
}
[ -- : 1씩 감소]
#include <iostream>
using namespace std;
int main() {
int x = 3;
--x;
cout << x; // 2
return 0;
}
PS.
출력 구문 안에서 ++, -- 증감연산자가 변수 뒤에 붙은 경우,
출력 구문 실행 후 값이 증감 됨.
(예제)
#include <iostream>
using namespace std;
int main() {
int a = 1;
cout << a++; // 1
cout << a; // 2
return 0;
}
할당연산자
값을 변수에 할당 (= 배정).
[ = : 할당]
[ += : 덧셈 후 할당]
[ -= : 뺄셈 후 할당]
[ *= : 곱셈 후 할당]
[ /= : 나눗셈 후 할당]
[ %= : 나눗셈 후, 나머지 할당] ★
[ &= : AND 비트 연산 후 할당]
[ |= : OR 비트 연산 후 할당]
[ ^= : XOR 비트 연산 후 할당]
[ >>= : Signed right shift 비트 연산 후 할당]
[ <<= : Zero fill left shift 비트 연산 후 할당]
[ = : 할당]
#include <iostream>
using namespace std;
int main() {
int x = 3;
cout << x; // 3
return 0;
}
[ += : 덧셈 후 할당]
#include <iostream>
using namespace std;
int main() {
int x = 5;
x += 3; // x = x + 3
cout << x; // 8
return 0;
}
[ -= : 뺄셈 후 할당]
#include <iostream>
using namespace std;
int main() {
int x = 5;
x -= 3; // x = x - 3
cout << x; // 2
return 0;
}
[ *= : 곱셈 후 할당]
#include <iostream>
using namespace std;
int main() {
int x = 5;
x *= 3; // x = x * 3
cout << x; // 15
return 0;
}
[ /= : 나눗셈 후 할당]
#include <iostream>
using namespace std;
int main() {
double x = 5;
x /= 3; // x = x / 3
cout << x; // 1.66667
return 0;
}
PS. 변수 x가 int 자료형 경우 결과값 : 1
[ %= : 나눗셈 후, 나머지 할당] ★
#include <iostream>
using namespace std;
int main() {
int x = 5;
x %= 3; // x = x % 3
cout << x; // 2
return 0;
}
[ &= : AND 비트 연산 후 할당]
※ 두 비트 비교해 모두 1이면 각 비트를 1로 설정 후 할당.
※ 5의 이진수 : 00000101
※ 3의 이진수 : 00000011
※ & 연산 시 : 00000001 (※ 이걸 10진수로 변환 시 1)
#include <iostream>
using namespace std;
int main() {
int x = 5;
x &= 3; // x = x & 3
cout << x;
return 0;
}
[ |= : OR 비트 연산 후 할당]
※ 두 비트 중 하나가 1이면 각 비트를 1로 설정 후 할당.
※ 5의 이진수 : 000001 01
※ 3의 이진수 : 0000001 1
※ |= 연산 시 : 00000111 (※ 이걸 10진수로 변환 시 7)
#include <iostream>
using namespace std;
int main() {
int x = 5;
x |= 3; // x = x | 3
cout << x; // 7
return 0;
}
[ ^= : XOR 비트 연산 후 할당]
※ 두 비트 중 하나만 1이면 각 비트를 1로 설정 후 할당.
※ 5의 이진수 : 000001 01
※ 3의 이진수 : 0000001 1
※ ^= 연산 시 : 0000011 0 (※ 이걸 10진수로 변환 시 6)
#include <iostream>
using namespace std;
int main() {
int x = 5;
x ^= 3; // x = x ^ 3
cout << x; // 6
return 0;
}
[ >>= : Signed right shift 비트 연산 후 할당]
※ 왼쪽 끝 비트 복사해 왼쪽에 밀어넣어 오른쪽으로 해당 숫자 만큼 이동 후, 오른쪽 끝 비트 제거.
※ 5의 이진수 00000101을 오른족으로 3칸 이동 시, 00000000 됨.
#include <iostream>
using namespace std;
int main() {
int x = 5;
x >>= 3; // x = x >> 3
cout << x; // 0
return 0;
}
[ <<= : Zero fill left shift 비트 연산 후 할당]
※ 오른쪽 끝에 0 밀어넣어 왼쪽으로 해당 숫자만큼 이동 후, 왼쪽 끝 비트 제거.
※ 5의 이진수 00000101을 왼쪽으로 3칸 이동 시, 00101000 됨.
※ 결국, 2의 5승 (= 32) + 2의 3승 (= 8) 더하면 40 됨.
#include <iostream>
using namespace std;
int main() {
int x = 5;
x <<= 3; // x = x << 3
cout << x; // 40
return 0;
}
비교연산자
두 값을 비교. (= 두 값의 관계)
[ == : 같음]
[ != : 같지 않음]
[ > : 더 큼]
[ < : 더 작음]
[ >= : 더 크거나 같음]
[ <= : 더 작거나 같음]
[ == : 같음]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x == y); // 0 (∵ false)
return 0;
}
[ != : 같지 않음]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x != y); // 1 (∵ true)
return 0;
}
[ > : 더 큼]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > y); // 1 (∵ true)
return 0;
}
[ < : 더 작음]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x < y); // 0 (∵ false)
return 0;
}
[ >= : 더 크거나 같음]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x >= y); // 1 (∵ true)
return 0;
}
[ <= : 더 작거나 같음]
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x <= y); // 0 (∵ false)
return 0;
}
논리연산자
조건 구문 결합.(※ 소문자 로 기재.)
[ && : 그리고]
[ || : 또는]
[ ! : 결과의 역 반환]
[ && : 그리고]
※ 모두 true여야 true 반환.
※ && 대신 소문자 and 표기해도 정상 작동. (cf. Java는 에러 남.)
※ 3 < x < 10 처럼 표현하면 에러 발생. 아래치럼 나눠서 표현.
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << (x > 3 && x < 10); // 1
return 0;
}
[ || : 또는]
※ 하나라도 true이면 true 반환.
※ || 대신 소문자 or 표기해도 정상 작동. (cf. Java는 에러 남.)
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << (x > 3 || x < 4); // 1
return 0;
}
[ ! : 결과의 역 반환]
※ true이면 false 반환.
※ false이면 true 반환.
※ ! 대신 소문자 not 표기해도 정상 작동. (cf. Java는 에러 남.)
※ &&・|| 연산자보다 우선.
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << (!(x > 3 && x < 10)); // 0
return 0;
}
비트연산자
2진수로 변환 후, 8자리 비트로 표현해 연산.
※ 6가지 존재. (∵ Java 엔 있는 >>> 연산자는 지원 X)
[ & : AND]
[ | : OR]
[ ^ : XOR]
[ ~ : NOT]
[ >> : Signed right shift (※ 오른쪽 이동)]
[ << : Zero fill left shift (※ 왼쪽 이동)]
PS.
※ 비트연산자는 어렵지만, 거의 사용 안 함. ㅡㅡ;
[ & : AND]
두 비트가 모두 1이면 각 비트를 1로 설정.
(예) 5 & 1 → 0101 & 0001 → 0001 → 1
[ | : OR]
두 비트 중 하나가 1이면 각 비트를 1로 설정.
(예) 5 | 1 → 0101 | 0001 → 0101 → 5
[ ^ : XOR]
두 비트 중 하나만 1이면 각 비트를 1로 설정.
(예) 5 ^ 1 → 0101 ^ 0001 → 0100 → 4
[ ~ : NOT]
모든 비트 반전. (즉, 1 → 0, 0 → 1 변환)
(예) ~ 5 → ~0101 → 1010 → 10
[ >> : Signed right shift (※ 오른쪽 이동)]
왼쪽에 맨 왼쪽 비트의 복사본을 밀어 넣어 오른쪽으로 이동시켜 맨 오른쪽 비트 제거.
(예) 9 >> 1 → 1001 >> 1 → 1100 → 12
[ << : Zero fill left shift (※ 왼쪽 이동)]
오른쪽에 0을 밀어넣어 왼쪽으로 이동시켜, 가장 왼쪽 비트 제거.
(예) 9 << 1 → 1001 << 1 → 0010 → 2
연산자 우선순위
(산술 > 비교 > ! 논리 > && 및 || 논리 )연산자 순.
PS. 산술연산자 경우
1순위 : * (곱셈) , / (나눗셈) , // (몫) , % (나머지)
2순위 : + (덧셈), - (뺄셈)
[예제]
#include <iostream>
using namespace std;
int main() {
cout << (3*4 > 3+4 && !(3*4>10)); // 0
return 0;
}
주소 복사
랜덤 이동
최신댓글