2進数でダンプしたくて調べていたら、std::bitset<N>
なんてものがあるなんて知らなかった。ついでに使い方の備忘録を残しておく。
// test.cpp
#include <iostream>
#include <bitset>
using namespace std;
int main(int argc, char *argv[]){
int8_t val = 0xa2; // 8bit 162, 10100010
int16_t val2 = 0xaaaa; // 16bit 43690, 1010101010101010
bitset<8> b1(val);
bitset<16> b2(val2);
bitset<32> b3(val2);
bitset<64> b4(val2);
bitset<8> b5("00001111");
// デフォルトが4バイトなので64bitからサイズアップ
cout<<"[sizeof]"<<endl;
cout<<"b1 = "<< sizeof(b1) <<endl;
cout<<"b2 = "<< sizeof(b2) <<endl;
cout<<"b3 = "<< sizeof(b3) <<endl;
cout<<"b4 = "<< sizeof(b4) <<endl;
cout<<"b5 = "<< sizeof(b5) <<endl;
// テンプレートで指定したbitサイズ
cout<<"[size()]"<<endl;
cout<<"b1 = "<< b1.size() <<endl;
cout<<"b2 = "<< b2.size() <<endl;
cout<<"b3 = "<< b3.size() <<endl;
cout<<"b4 = "<< b4.size() <<endl;
cout<<"b5 = "<< b5.size() <<endl;
// 表示
cout<<"[bit]"<<endl;
cout<<"b1 = "<< b1 <<endl;
cout<<"b2 = "<< b2 <<endl;
cout<<"b3 = "<< b3 <<endl;
cout<<"b4 = "<< b4 <<endl;
cout<<"b5 = "<< b5 <<endl;
// 各bitへのアクセスは配列と同じ
cout<<"---------------"<<endl;
cout<<"7 6 5 4 3 2 1 0"<<endl;
cout<<"---------------"<<endl;
for(int i = b1.size()-1; i>=0; i--){
cout<<b1[i]<<" ";
}
cout<<endl;
// 0bit => 1
b1.set(0);
cout<<"0bit => 1, b1 = "<< b1 <<endl;
// 7bit => 0
b1.reset(7);
cout<<"7bit => 0, b1 = "<< b1 <<endl;
// test
cout<<"0bit=1 ?, b1 = "<< b1.test(0) <<endl;
cout<<"7bit=1 ?, b1 = "<< b1.test(7) <<endl;
// bitset -> unsinged long
cout<<"to_ulong(), b1 = "<< b1.to_ulong() <<" sizeof = "
<<sizeof(b1.to_ulong())<<endl;
// bitset -> unsinged long long
cout<<"to_ullong(), b4 = "<< b4.to_ullong() <<" sizeof = "
<<sizeof(b4.to_ullong())<<endl;
// bitset -> string
cout<<"to_string(), b1 = "<< b1.to_string() <<" sizeof = "
<<b1.to_string().size()<<endl;
// 反転
cout<<"flip(), b1 = "<< b1.flip() <<endl;
// 1つでもbitがセットされているか
cout<<"any(), b1 = "<< b1.any() <<endl;
cout<<"none(), b1 = "<< b1.none() <<endl;
// セットされているbitの数
cout<<"count(), b1 = "<< b1.count() <<endl;
// すべてのビットを立てる
cout<<"set(), b1 = "<< b1.set() <<endl;
cout<<"all(), b1 = "<< b1.all() <<endl;
// すべてのビットを0
cout<<"reset(), b1 = "<< b1.reset() <<endl;
cout<<"none(), b1 = "<< b1.none() <<endl;
// 演算
bitset<8> x("10100010");
bitset<8> y("11110000");
cout<<"x = "<< x <<endl;
cout<<"y = "<< y <<endl;
cout<<"x AND y "<< (x & y) <<endl;
cout<<"x OR y "<< (x | y) <<endl;
cout<<"x XOR y "<< (x ^ y) <<endl;
// シフト
cout<<"x << 1 "<< (x << 1) <<endl;
cout<<"x >> 2 "<< (x >> 2) <<endl;
cout<<"y << 2 >> 6 "<< (y << 2 >> 6) <<endl;
// 比較
cout<<"x == y "<< (x == y) <<endl;
return 0;
}
ビルドして実行した結果が以下になる。
> gcc -o test test.cpp -lstdc++ && ./test
[sizeof]
b1 = 4
b2 = 4
b3 = 4
b4 = 8
b5 = 4
[size()]
b1 = 8
b2 = 16
b3 = 32
b4 = 64
b5 = 8
[bit]
b1 = 10100010
b2 = 1010101010101010
b3 = 11111111111111111010101010101010
b4 = 1111111111111111111111111111111111111111111111111010101010101010
b5 = 00001111
---------------
7 6 5 4 3 2 1 0
---------------
1 0 1 0 0 0 1 0
0bit => 1, b1 = 10100011
7bit => 0, b1 = 00100011
0bit=1 ?, b1 = 1
7bit=1 ?, b1 = 0
to_ulong(), b1 = 35 sizeof = 4
to_ullong(), b4 = 18446744073709529770 sizeof = 8
to_string(), b1 = 00100011 sizeof = 8
flip(), b1 = 11011100
any(), b1 = 1
none(), b1 = 0
count(), b1 = 5
set(), b1 = 11111111
all(), b1 = 1
reset(), b1 = 00000000
none(), b1 = 1
x = 10100010
y = 11110000
x AND y 10100000
x OR y 11110010
x XOR y 01010010
x << 1 01000100
x >> 2 00101000
y << 2 >> 6 00000011
x == y 0