ビット演算してみる (C++)
C++
Published: 2019-10-01

やったこと

ビット演算してみます。

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a = 1;
    cout << a << "\n";
    cout << std::bitset<8>(a) << "\n";

    int a2 = a << 2;
    cout << a2 << "\n";
    cout << std::bitset<8>(a2) << "\n";

    int a3 = a2 >> 1;
    cout << a3 << "\n";
    cout << std::bitset<8>(a3) << "\n";
}

出力結果

1
00000001
4
00000100
2
00000010

参考