NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
bit_count.hpp
Go to the documentation of this file.
1 #pragma once
29 
33 #include "NumCpp/NdArray.hpp"
34 
35 namespace 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 += (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:42
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1221
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4276
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1529
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:121
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
Definition: Coordinate.hpp:45
constexpr int bit_count(dtype inValue) noexcept
Definition: bit_count.hpp:45