Namespaces
Variants

std:: popcount

From cppreference.net
Utilities library
헤더에 정의됨 <bit>
template < class T >
constexpr int popcount ( T x ) noexcept ;
(C++20부터)

1 비트의 개수를 반환합니다 x 값에서.

이 오버로드는 T 가 부호 없는 정수 타입(즉, unsigned char , unsigned short , unsigned int , unsigned long , unsigned long long , 또는 확장 부호 없는 정수 타입)인 경우에만 오버로드 해결에 참여합니다.

목차

매개변수

x - 부호 없는 정수형의 값

반환값

1 비트의 개수 x 에서.

참고 사항

이름 popcount 는 "population count"의 약어입니다.

기능 테스트 매크로 표준 기능
__cpp_lib_bitops 201907L (C++20) 비트 연산

예제

#include <bit>
#include <bitset>
#include <cstdint>
#include <iostream>
static_assert(std::popcount(0xFULL) == 4);
int main()
{
    for (const std::uint8_t x : {0, 0b00011101, 0b11111111})
        std::cout << "popcount( " << std::bitset<8>(x) << " ) = "
                  << std::popcount(x) << '\n';
}

출력:

popcount( 00000000 ) = 0
popcount( 00011101 ) = 4
popcount( 11111111 ) = 8

참고 항목

최상위 비트부터 시작하여 연속된 0 비트의 개수를 셉니다
(함수 템플릿)
(C++20)
최상위 비트부터 시작하여 연속된 1 비트의 개수를 셉니다
(함수 템플릿)
최하위 비트부터 시작하여 연속된 0 비트의 개수를 셉니다
(함수 템플릿)
(C++20)
최하위 비트부터 시작하여 연속된 1 비트의 개수를 셉니다
(함수 템플릿)
숫자가 2의 정수 거듭제곱인지 확인합니다
(함수 템플릿)
true 로 설정된 비트의 개수를 반환합니다
( std::bitset<N> 의 public 멤버 함수)
모든 비트, 일부 비트, 또는 어떤 비트도 true 로 설정되었는지 확인합니다
( std::bitset<N> 의 public 멤버 함수)