numeric_bit_compress
numeric_bit_compress (std::bit_compress)
<bit> 헤더에 정의되어 있어요. mask에 1비트가 있는 x의 비트들을 선택해 오른쪽으로 연속하게 압축(pack)해요. 나머지 비트는 0이에요. C++29부터 도입됐어요.
출처: cppreference
본문
template< class T >
constexpr T bit_compress( T x, T mask ) noexcept;
(C++29부터)
mask에 1비트가 있는 x의 비트들을 선택해 오른쪽으로 연속하게 압축해요. 나머지 비트는 0이에요.
매개변수
x: 압축할 소스 값mask: 압축에 사용할 비트 마스크
타입 요구사항
T는 부호 없는 정수 타입(즉unsigned char,unsigned short,unsigned int,unsigned long,unsigned long long또는 확장 부호 없는 정수 타입)이어야 오버로드 해석에 참여해요.
반환값
마스크 mask로 비트 압축을 적용한 x.
주의 (Notes)
이 함수는 x86-64의 PEXT 명령과 ARM의 BEXT 명령과 같은 결과를 내요.
예제
#include <bit>
#include <cstdint>
int main()
{
// mask 의 1비트 위치에 해당하는 source 비트만 골라 오른쪽으로 압축
static_assert(
std::bit_compress(
std::uint16_t{0xABCD}, // source
std::uint16_t{0x0F0F}) // mask
== std::uint16_t{0x00BD} // result
);
}