NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
bit_count.hpp
Go to the documentation of this file.
1
28#pragma once
29
33#include "NumCpp/NdArray.hpp"
34
35namespace nc
36{
37 //============================================================================
38 // Method Description:
44 template<typename dtype>
45 constexpr int bit_count(dtype inValue) noexcept
46 {
48
49 int count = 0;
50 for (int bit = 0; bit < DtypeInfo<dtype>::bits(); ++bit)
51 {
52 count += static_cast<int>((inValue & (dtype{ 1 } << bit)) >> bit);
53 }
54
55 return count;
56 }
57
58 //============================================================================
59 // Method Description:
65 template<typename dtype>
67 {
68 NdArray<int> returnArray(inArray.shape());
69
71 inArray.cend(),
72 returnArray.begin(),
73 [](dtype inValue) noexcept -> int { return bit_count(inValue); });
74
75 return returnArray;
76 }
77} // namespace nc
#define STATIC_ASSERT_UNSIGNED_INTEGER(dtype)
Definition: StaticAsserts.hpp:46
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:119
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:775
Definition: Cartesian.hpp:40
constexpr int bit_count(dtype inValue) noexcept
Definition: bit_count.hpp:45