std::bitset
From cppreference.com
| ヘッダー <bitset> で定義 |
||
| template< std::size_t N > class bitset; |
||
クラステンプレート bitset は、N ビットの固定長シーケンスを表す。ビットセットは、標準の論理演算子で操作でき、文字列や整数との間で相互変換できる。文字列表現とシフト演算の方向を命名する目的上、このシーケンスは整数の2進数表現のように、最もインデックスが低い要素が右にあると見なされる。
bitset は CopyConstructible と CopyAssignable の要件を満たす。
|
|
(C++23から) |
目次 |
[編集] テンプレートパラメータ
| N | - | ストレージを確保するビット数 |
[編集] メンバ型
| 1つのビットへの参照を表すプロキシクラス (クラス) |
[編集] メンバ関数
| ビットセットを構築する (public member function) | |
| (C++20で削除) |
内容を比較する (public member function) |
要素アクセス | |
| 特定のビットにアクセスする (public member function) | |
| 特定のビットにアクセスする (public member function) | |
| 全てのビット、いずれかのビット、またはいずれのビットも true に設定されているかをチェックする (public member function) | |
| true に設定されているビットの数を返す (public member function) | |
容量 | |
| ビットセットが保持するビット数を返す (public member function) | |
変更 | |
| ビット単位のAND、OR、XOR、NOTを実行する (public member function) | |
| ビット単位の左シフトと右シフトを実行する (public member function) | |
| ビットを true または指定された値に設定する (public member function) | |
| ビットを false に設定する (public member function) | |
| ビットの値を反転させる (public member function) | |
型変換 | |
| データの文字列表現を返す (public member function) | |
| データの unsigned long 整数表現を返す (public member function) | |
| (C++11) |
データの unsigned long long 整数表現を返す (public member function) |
[編集] 非メンバ関数
| ビットセットに対するビット単位の論理演算を実行する (function template) | |
| ビットセットのストリーム入出力を実行する (function template) |
[編集] ヘルパークラス
| (C++11) |
std::bitset のハッシュサポート (class template specialization) |
[編集] ノート
ビットセットのサイズがコンパイル時に不明な場合や、実行時にサイズを変更する必要がある場合は、std::vector<bool> や boost::dynamic_bitset<> のような動的な型を代わりに使用することができる。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_constexpr_bitset |
202207L |
(C++23) | よりconstexprな std::bitset |
__cpp_lib_bitset |
202306L |
(C++26) | std::bitset と std::string_view の連携 |
[編集] 例
このコードを実行
#include <bitset> #include <cassert> #include <cstddef> #include <iostream> int main() { typedef std::size_t length_t, position_t; // the hints // constructors: constexpr std::bitset<4> b1; constexpr std::bitset<4> b2{0xA}; // == 0B1010 std::bitset<4> b3{"0011"}; // can also be constexpr since C++23 std::bitset<8> b4{"ABBA", length_t(4), /*0:*/'A', /*1:*/'B'}; // == 0B0000'0110 // bitsets can be printed out to a stream: std::cout << "b1:" << b1 << "; b2:" << b2 << "; b3:" << b3 << "; b4:" << b4 << '\n'; // bitset supports bitwise operations: b3 |= 0b0100; assert(b3 == 0b0111); b3 &= 0b0011; assert(b3 == 0b0011); b3 ^= std::bitset<4>{0b1100}; assert(b3 == 0b1111); // operations on the whole set: b3.reset(); assert(b3 == 0); b3.set(); assert(b3 == 0b1111); assert(b3.all() && b3.any() && !b3.none()); b3.flip(); assert(b3 == 0); // operations on individual bits: b3.set(position_t(1), true); assert(b3 == 0b0010); b3.set(position_t(1), false); assert(b3 == 0); b3.flip(position_t(2)); assert(b3 == 0b0100); b3.reset(position_t(2)); assert(b3 == 0); // subscript operator[] is supported: b3[2] = true; assert(true == b3[2]); // other operations: assert(b3.count() == 1); assert(b3.size() == 4); assert(b3.to_ullong() == 0b0100ULL); assert(b3.to_string() == "0100"); }
出力
b1:0000; b2:1010; b3:0011; b4:00000110
[編集] 関連項目
| 空間効率の良い動的ビットセット (class template specialization) | |
| ビット操作 (C++20) | 個々のビットおよびビットシーケンスへのアクセス、操作、処理を行うためのユーティリティ |