std:: bitset
|
헤더 파일에 정의됨
<bitset>
|
||
|
template
<
std::
size_t
N
>
class bitset ; |
||
클래스 템플릿
bitset
은 고정된 크기의
N
비트 시퀀스를 나타냅니다. 비트셋은 표준 논리 연산자로 조작될 수 있으며 문자열 및 정수와 상호 변환 가능합니다. 문자열 표현 및 시프트 연산 방향 지정의 목적상, 이 시퀀스는 정수의 이진 표현에서와 같이 가장 낮은 인덱스를 가진 요소가
오른쪽
에 위치하는 것으로 간주됩니다.
bitset
는
CopyConstructible
와
CopyAssignable
요구 사항을 충족합니다.
|
|
(since C++23) |
목차 |
템플릿 매개변수
| N | - | 저장 공간을 할당할 비트 수 |
멤버 타입
|
비트에 대한 참조를 나타내는 프록시 클래스
(클래스) |
멤버 함수
|
bitset을 생성함
(public member function) |
|
|
(removed in C++20)
|
내용을 비교함
(public member function) |
요소 접근 |
|
|
특정 비트에 접근함
(public member function) |
|
|
특정 비트에 접근함
(public member function) |
|
|
모든 비트, 어떤 비트, 또는 어떤 비트도
true
로 설정되었는지 확인함
(public member function) |
|
|
true
로 설정된 비트 수를 반환함
(public member function) |
|
용량 |
|
|
bitset이 보유하는 비트 수를 반환함
(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) |
비멤버 함수
|
bitset에 대한 이진 논리 연산 수행
(function template) |
|
|
bitset의 스트림 입력 및 출력 수행
(function template) |
헬퍼 클래스
|
(C++11)
|
std::bitset
에 대한 해시 지원
(클래스 템플릿 특수화) |
참고 사항
비트 집합의 크기를 컴파일 타임에 알 수 없거나 런타임에 크기를 변경해야 하는 경우,
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; // 힌트 // 생성자: constexpr std::bitset<4> b1; constexpr std::bitset<4> b2{0xA}; // == 0B1010 std::bitset<4> b3{"0011"}; // C++23부터 constexpr로 사용 가능 std::bitset<8> b4{"ABBA", length_t(4), /*0:*/'A', /*1:*/'B'}; // == 0B0000'0110 // bitset은 스트림으로 출력 가능: std::cout << "b1:" << b1 << "; b2:" << b2 << "; b3:" << b3 << "; b4:" << b4 << '\n'; // bitset은 비트 연산을 지원: b3 |= 0b0100; assert(b3 == 0b0111); b3 &= 0b0011; assert(b3 == 0b0011); b3 ^= std::bitset<4>{0b1100}; assert(b3 == 0b1111); // 전체 집합에 대한 연산: b3.reset(); assert(b3 == 0); b3.set(); assert(b3 == 0b1111); assert(b3.all() && b3.any() && !b3.none()); b3.flip(); assert(b3 == 0); // 개별 비트에 대한 연산: 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); // 첨자 연산자 [] 지원: b3[2] = true; assert(true == b3[2]); // 기타 연산: 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
참고 항목
|
공간 효율적인 동적 비트셋
(클래스 템플릿 특수화) |
|
| Bit manipulation (C++20) | 개별 비트 및 비트 시퀀스에 접근, 조작 및 처리를 위한 유틸리티 |