Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/popcount"

From cppreference.com
< cpp‎ | numeric
m (See also: ++)
m (~ fmt)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{cpp/title|popcount}}
 
{{cpp/title|popcount}}
 
{{cpp/numeric/navbar}}
 
{{cpp/numeric/navbar}}
{{dcl begin}}
+
{{ddcl|header=bit|since=c++20|
{{dcl header | bit}}
+
template< class T >
{{dcl | since=c++20 |
+
constexpr int popcount( T x ) noexcept;
template<class T>
+
constexpr int popcount(T x) noexcept;
+
 
}}
 
}}
{{dcl end}}
 
  
Returns the number of 1 bits in the value of {{tt|x}}.
+
Returns the number of {{c|1}} bits in the value of {{c|x}}.
  
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c|unsigned char}}, {{c|unsigned short}}, {{c|unsigned int}}, {{c|unsigned long}}, {{c|unsigned long long}}, or an extended unsigned integer type)}}.
+
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c/core|unsigned char}}, {{c/core|unsigned short}}, {{c/core|unsigned int}}, {{c/core|unsigned long}}, {{c/core|unsigned long long}}, or an extended unsigned integer type)}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | x | values of unsigned integer type}}
+
{{par|x|value of unsigned integer type}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
The number of 1 bits in the value of x.
+
The number of 1 bits in the value of {{c|x}}.
  
=== Example ===
+
===Notes===
 +
{{petty|The name {{tt|popcount}} is a contraction for "population count".}}
 +
 
 +
{{feature test macro|__cpp_lib_bitops|std=C++20|value=201907L|[[cpp/numeric#Bit manipulation (since C++20)|Bit operations]]}}
 +
 
 +
===Example===
 
{{example
 
{{example
 
|code=
 
|code=
Line 27: Line 29:
 
#include <bitset>
 
#include <bitset>
 
#include <cstdint>
 
#include <cstdint>
#include <initializer_list>
 
 
#include <iostream>
 
#include <iostream>
 +
 +
static_assert(std::popcount(0xFULL) == 4);
  
 
int main()
 
int main()
 
{
 
{
     for (std::uint8_t i : { 0, 0b11111111, 0b00011101 }) {
+
     for (const std::uint8_t x : {0, 0b00011101, 0b11111111})
         std::cout << "popcount(0b" << std::bitset<8>(i) << ") = "
+
         std::cout << "popcount( " << std::bitset<8>(x) << " ) = "
                   << std::popcount(i) << '\n';
+
                   << std::popcount(x) << '\n';
    }
+
 
}
 
}
 
|output=
 
|output=
popcount(0b00000000) = 0
+
popcount( 00000000 ) = 0
popcount(0b11111111) = 8
+
popcount( 00011101 ) = 4
popcount(0b00011101) = 4
+
popcount( 11111111 ) = 8
 
}}
 
}}
  
=== See also ===
+
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/dsc countl_zero}}
+
{{dsc inc|cpp/numeric/dsc countl_zero}}
{{dsc inc | cpp/numeric/dsc countl_one}}
+
{{dsc inc|cpp/numeric/dsc countl_one}}
{{dsc inc | cpp/numeric/dsc countr_zero}}
+
{{dsc inc|cpp/numeric/dsc countr_zero}}
{{dsc inc | cpp/numeric/dsc countr_one}}
+
{{dsc inc|cpp/numeric/dsc countr_one}}
{{dsc inc | cpp/numeric/dsc has_single_bit}}
+
{{dsc inc|cpp/numeric/dsc has_single_bit}}
{{dsc inc | cpp/utility/bitset/dsc count}}
+
{{dsc inc|cpp/utility/bitset/dsc count}}
{{dsc inc | cpp/utility/bitset/dsc all_any_none}}
+
{{dsc inc|cpp/utility/bitset/dsc all_any_none}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|es|ja|ru|zh}}
 
{{langlinks|es|ja|ru|zh}}

Latest revision as of 14:06, 2 November 2024

 
 
 
Defined in header <bit>
template< class T >
constexpr int popcount( T x ) noexcept;
(since C++20)

Returns the number of 1 bits in the value of x.

This overload participates in overload resolution only if T is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Contents

[edit] Parameters

x - value of unsigned integer type

[edit] Return value

The number of 1 bits in the value of x.

[edit] Notes

The name popcount is a contraction for "population count".

Feature-test macro Value Std Feature
__cpp_lib_bitops 201907L (C++20) Bit operations

[edit] Example

#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';
}

Output:

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

[edit] See also

counts the number of consecutive 0 bits, starting from the most significant bit
(function template) [edit]
counts the number of consecutive 1 bits, starting from the most significant bit
(function template) [edit]
counts the number of consecutive 0 bits, starting from the least significant bit
(function template) [edit]
counts the number of consecutive 1 bits, starting from the least significant bit
(function template) [edit]
checks if a number is an integral power of 2
(function template) [edit]
returns the number of bits set to true
(public member function of std::bitset<N>) [edit]
checks if all, any or none of the bits are set to true
(public member function of std::bitset<N>) [edit]