비트(Bit) 단위로 논리 연산을 할 때 사용하는 연산자.
할당된 비트 전체를 왼쪽 · 오른쪽으로 이동시키는 연산 역시 가능하다.
Bitwise Operation is an operator used to perform bit-level logical operation.
It is also possible to perform operations that shift the entire assigned bit to the left or right.
e.g.) a = 1010, b = 0100
Operator | Name | Example |
---|---|---|
& | AND | a & b = 0000 |
| | OR | a | b = 1110 |
^ | XOR | a ^ b = 1110 |
~ | NOT | ~a = 0101 |
<< | Left Shift | a << n == a * 2^n |
>> | Right Shift | a >> n == a 2(-n) |
사칙연산은 비교, 논리 연산자보다 우선순위가 높다.
반면 Shift 제외, 비트연산의 우선순위는 논리연산보다 높고, 비교연산보다 낮다.
Arithmetic operators have a higher priority than comparison and logical operators.
On the other hand, the priority of bitwise operators, excluding shift, is higher than the logical operators and lower than the comparison operators.
( +, -, *, / ) Arithmetic operators
> ( ==, >=, < ) comparison operators
> ( &, | , ^ ) bitwise operators
> ( &&, || ) logical operators
// wrong.
if (x & y == 0) // == if (x & (y == 0))
// right.
if ((x & y) == 0)