O.2 Bitwise operators

주홍영·2022년 3월 12일
0

Learncpp.com

목록 보기
57/199

https://www.learncpp.com/cpp-tutorial/bitwise-operators/

c++은 다음과 같은 6개의 bit manipulation operators를 제공한다

참고로 bitset 자료형을 사용하는 경우 std::cout으로 출력했을 때
별도의 조작없이도 2진수 형태로 출력해준다

예시를 보자

#include <bitset>
#include <iostream>

int main()
{
    std::bitset<4> x { 0b1100 };

    std::cout << x << '\n';
    std::cout << (x >> 1) << '\n'; // shift right by 1, yielding 0110
    std::cout << (x << 1) << '\n'; // shift left by 1, yielding 1000

    return 0;
}

출력

1100
0110
1000

추가적인 내용으로 +=, -=와 같은 연산 후 할당도 가능하다
기호는 다음과 같다

만약 x >>= 1 이라면 x = x >> 1; 과 같은 의미를 가진다

profile
청룡동거주민

0개의 댓글