비트 연산자

Jaemyeong Lee·2024년 7월 31일
0

FastCampusC++

목록 보기
13/78

비트 연산자 코드 및 주석

#include <iostream>
#include <cmath> // pow 함수를 사용하기 위해 포함

using namespace std;

int main()
{
    {
        // Not 연산자(~)
        // 0 -> 1
        // 1 -> 0
    }
    {
        // 0000,0000,0000,0000,0000,0000,0000,0000 (0)
        int num = 0;

        // 1111,1111,1111,1111,1111,1111,1111,1111 (-1)
        cout << ~num << endl;
    }
    {
        // 0000,0000,0000,0000,0000,0000,0000,0000 (0)
        unsigned int num = 0;

        // 1111,1111,1111,1111,1111,1111,1111,1111 (2^32 - 1)
        cout << ~num << endl;
    }
    {
        // 0000,0000,0000,0000,0000,0000,0000,0001 (1)
        unsigned int num = 1;

        // 1111,1111,1111,1111,1111,1111,1111,1110 (2^32 - 2)
        cout << ~num << endl;

        // 0000,0000,0000,0000,0000,0000,0000,0001 (1)
        cout << ~~num << endl;
    }
    {
        // AND 연산자(&)
        // 1 & 1 == 1
        // 1 & 0 == 0
        // 0 & 1 == 0
        // 0 & 0 == 0
    }
    {
        int num0 = 5;
        int num1 = 10;

        int result = num0 & num1;
        cout << result << endl;

        // 0000,0101 == 5 == 4 + 1 == 2^2 + 2^0
        // 0000,1010 == 10 == 8 + 2 == 2^3 + 2^1
        // 0000,0000 == 0
    }
    {
        int num0 = 5;
        int num1 = 11;

        int result = num0 & num1;
        cout << result << endl;

        // 0000,0101 == 5 == 4 + 1 == 2^2 + 2^0
        // 0000,1011 == 11 == 8 + 2 + 1 == 2^3 + 2^1 + 2^0
        // 0000,0001 == 1
    }
    {
        // AND 연산자를 이용하여 flag가 존재하는지 체크
        uint32_t attendanceBook = 5;

        if (attendanceBook & (int)pow(2, 0))
            cout << "1st" << endl;

        if (attendanceBook & (int)pow(2, 1))
            cout << "2nd" << endl;

        if (attendanceBook & (int)pow(2, 2))
            cout << "3rd" << endl;

        if (attendanceBook & ((int)pow(2, 0) + (int)pow(2, 1)))
            cout << "1st or 2nd" << endl;

        int flag = (int)pow(2, 0) + (int)pow(2, 2);
        if ((attendanceBook & flag) == flag)
            cout << "1st and 3rd" << endl;

        // 0000,0000,0000,0000,0000,0000,0000,0101 = 5

        // 0000,0000,0000,0000,0000,0000,0000,0001 = 1 AND // 1st day checked
        // 0000,0000,0000,0000,0000,0000,0000,0001 = 1 // 1st day present

        // 0000,0000,0000,0000,0000,0000,0000,0010 = 2 AND // 2nd day checked
        // 0000,0000,0000,0000,0000,0000,0000,0000 = 0 // 2nd day absent
    }
    {
        // OR 연산자(|)
        // 1 | 1 == 1
        // 1 | 0 == 1
        // 0 | 1 == 1
        // 0 | 0 == 0
    }
    {
        // OR 연산자를 이용하여 flag를 추가
        uint32_t attendanceBook = 0u;

        // 첫째 날 출석 처리
        attendanceBook |= (int)pow(2, 0);
        cout << attendanceBook << endl;

        // 중복해서 같은 날에 출석을 해도 문제가 없다.
        attendanceBook |= (int)pow(2, 0);
        cout << attendanceBook << endl;

        // 0000,0000,0000,0000,0000,0000,0000,0000 = 0
        // 0000,0000,0000,0000,0000,0000,0000,0001 = 1 OR // 1st day present
        // 0000,0000,0000,0000,0000,0000,0000,0001 = 1
    }
    {
        uint32_t attendanceBook = 5u;

        // 셋째 날 결석 처리
        attendanceBook &= ~((int)pow(2, 2));

        // 0000,0000,0000,0000,0000,0000,0000,0101 = 5
        // 1111,1111,1111,1111,1111,1111,1111,1011 AND
        // 0000,0000,0000,0000,0000,0000,0000,0001 = 1
    }
    {
        // XOR 연산자(^)
        // 1 ^ 1 == 0
        // 1 ^ 0 == 1
        // 0 ^ 1 == 1
        // 0 ^ 0 == 0
    }
    {
        int num0 = 5;
        int num1 = 10;

        cout << (num0 ^ num1) << endl;

        // 0000,0101 = 5
        // 0000,1010 = 10
        // 0000,1111 = 15
    }
    {
        // XOR를 이용하여 출석/결석 토글
        uint32_t attendanceBook = 0u;

        attendanceBook ^= (int)pow(2, 0); // 0001
        cout << attendanceBook << endl;

        attendanceBook ^= (int)pow(2, 0); // 0001 ^ 0001 == 0000
        cout << attendanceBook << endl;

        attendanceBook ^= (int)pow(2, 0); // 0000 ^ 0001 == 0001
        cout << attendanceBook << endl;
    }
    {
        // 홀수번 있는 단 한개의 수를 구하기
        int a = 1, b = 3, c = 2, d = 1, e = 2;
        cout << (a ^ b ^ c ^ d ^ e) << endl;
        cout << (a ^ d ^ c ^ e ^ b) << endl; // 교환 법칙
    }
    {
        // << 연산자 (left shift)
        // 0000,0001 << 3
        // 0000,1000
        cout << (1 << 3) << endl;

        // << 한 번 할 때마다 2배
        cout << (1 << 0) << endl;
        cout << (1 << 1) << endl;
        cout << (1 << 2) << endl;
        cout << (1 << 3) << endl;

        // 값의 잘림(오버플로우)
        int num = 2200000000;
        num <<= 2;

        cout << num << endl;

        // pow 연산을 사용하지 않고 출석 처리
        int attendanceBook = 0u;
        attendanceBook |= (1 << 2);
        cout << attendanceBook << endl;
    }
    {
        // >> 연산자 (right shift)
        // 값의 잘림(언더플로우)
        cout << (1 >> 1) << endl;

        // >> 한 번 할 때마다 절반
        cout << (16 >> 1) << endl;
        cout << (8 >> 1) << endl;
        cout << (4 >> 1) << endl;
        cout << (2 >> 1) << endl;
        cout << (1 >> 1) << endl;

        cout << (14

 >> 1) << endl; // 7
        cout << (15 >> 1) << endl; // 7 (잘려서 나머지 없어짐)

        cout << (14 >> 2) << endl; // 3 (잘려서 나머지 없어짐)
        cout << (15 >> 2) << endl; // 3 (잘려서 나머지 없어짐)
    }
}

비트 연산자 요약 표

연산자설명예시결과
~비트 NOT, 각 비트를 반전~01111...1111 (-1)
&비트 AND, 대응하는 비트가 모두 1이면 15 & 100000...0000 (0)
``비트 OR, 대응하는 비트 중 하나라도 1이면 1`5
^비트 XOR, 대응하는 비트가 다르면 15 ^ 100000...1111 (15)
<<비트 왼쪽 시프트, 각 비트를 왼쪽으로 이동1 << 30000...1000 (8)
>>비트 오른쪽 시프트, 각 비트를 오른쪽으로 이동16 >> 10000...1000 (8)

주요 개념 설명

  1. 비트 NOT (~) 연산자:

    • 각 비트를 반전시킵니다. 0은 1로, 1은 0으로 변환합니다.
    • 예시: ~01111...1111 (2^32-1)로 변환됩니다.
  2. 비트 AND (&) 연산자:

    • 대응하는 비트가 모두 1이면 1을 반환합니다.
    • 예시: 5 & 100000...0000 (0)입니다.
  3. 비트 OR (|) 연산자:

    • 대응하는 비트 중 하나라도 1이면 1을 반환합니다.
    • 예시: 5 | 100000...1111 (15)입니다.
  4. 비트 XOR (^) 연산자:

    • 대응하는 비트가 다르면 1을 반환합니다.
    • 예시: 5 ^ 100000...1111 (15)입니다.
  5. 비트 왼쪽 시프트 (<<) 연산자:

    • 각 비트를 왼쪽으로 이동시키고 오른쪽에 0을 추가합니다. 각 이동은 2배를 의미합니다.
    • 예시: 1 << 30000...1000 (8)입니다.
  6. 비트 오른쪽 시프트 (>>) 연산자:

    • 각 비트를 오른쪽으로 이동시키고 왼쪽에 0을 추가합니다. 각 이동은 절반을 의미합니다.
    • 예시: 16 >> 10000...1000 (8)입니다.

주요 사용 예시

  1. AND 연산자를 이용한 flag 체크:

    uint32_t attendanceBook = 5;
    if (attendanceBook & (int)pow(2, 0))
        cout << "1st" << endl;
  2. OR 연산자를 이용한 flag 추가:

    uint32_t attendanceBook = 0u;
    attendanceBook |= (int)pow(2, 0);
    cout << attendanceBook << endl;
  3. XOR 연산자를 이용한 출석/결석 토글:

    uint32_t attendanceBook = 0u;
    attendanceBook ^= (int)pow(2, 0);
    cout << attendanceBook << endl;
  4. 왼쪽 시프트 연산자를 이용한 출석 처리:

    int attendanceBook = 0u;
    attendanceBook |= (1 << 2);
    cout << attendanceBook << endl;
profile
李家네_공부방

0개의 댓글