# 비트연산자

### 코드

{% code title="비트 단위 논리곱(AND)을 구할 때" %}

```
(값1) & (값2)
```

{% endcode %}

{% code title="비트 단위 논리합(OR)을 구할 때" %}

```
(값1) | (값2)
```

{% endcode %}

{% code title="비트 단위 배타적 논리합(XOR)을 구할 때" %}

```
(값1) ^ (값2)
```

{% endcode %}

{% code title="비트 단위 부정(NOT)을 구할 때" %}

```
~(값1)
```

{% endcode %}

{% code title="왼쪽 비트 이동 연산자, 왼쪽 시프트 연산자" %}

```
(값) << (숫자)
```

{% endcode %}

{% code title="오른쪽 비트 이동 연산자, 오른쪽 시프트 연산자" %}

```
(값) >> (숫자)
```

{% endcode %}

***

### 설명

비트 연산자는 값을 2진수로 바꿔 각 자리끼리 계산합니다.

* AND(`&`) : 두 비트가 모두 1일 때만 1
* OR(`|`) : 둘 중 하나라도 1이면 1

  <figure><img src="/files/Y5D8LX4R7yoDcu85hr0F" alt=""><figcaption></figcaption></figure>
* XOR(`^`) : 두 비트가 서로 다르면 1
* NOT(`~`) : 0과 1을 뒤집음<br>

  <figure><img src="/files/2E3Ozt212KhKAzguRV1A" alt=""><figcaption></figcaption></figure>
* `<<` : 비트를 왼쪽으로 밀어 값이 커짐
* `>>` : 비트를 오른쪽으로 밀어 값이 작아짐

  <figure><img src="/files/vHYR4Uz04a6H79hRwNLq" alt=""><figcaption></figcaption></figure>

***

### 주의사항

* `>>`에서 이동 칸 수가 비트 길이보다 크면 0이 될 수 있습니다.
* 10진수가 아닌 값을 사용할 때는 `2진수 0011`처럼 작성합니다.

***

### 파이썬 대응 코드

```
a & b
a | b
a ^ b
~a
a << n
a >> n
```

***

### 예시

{% code title="예시 코드" %}

```
ㄱ = 2진수 0011
ㄴ = 2진수 0110

ㄱ & ㄴ 보여주기
ㄱ | ㄴ 보여주기
ㄱ ^ ㄴ 보여주기
~ㄱ 보여주기
ㄱ << 1 보여주기
ㄴ >> 2 보여주기
```

{% endcode %}

{% code title="예시 코드의 출력" %}

```
2   # 0011 & 0110 = 0010
7   # 0011 | 0110 = 0111
5   # 0011 ^ 0110 = 0101
12   # ~0011 = 1100
6   # 0011 << 1 = 0110
1   # 0110 >> 2 = 0001
```

{% endcode %}

***

### 검색 키워드

비트연산자, AND, OR, XOR, NOT, 논리합, 논리곱, 배타적논리합,  부정, 시프트, 왼쪽시프트, 오른쪽시프트


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codebook.horang.it/readme/undefined-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
