Skip to content

Commit aae1e46

Browse files
committed
added bitmask type to ..math/utils/integer_traits.hpp
1 parent 139c572 commit aae1e46

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/modm/math/utils/integer_traits.hpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Thomas Sommer
2+
* Copyright (c) 2021-2022, Thomas Sommer
33
*
44
* This file is part of the modm project.
55
*
@@ -32,6 +32,7 @@ struct uint_t
3232
(Bits <= 64), uint64_t>>>>;
3333
};
3434

35+
/// ETL also got such a trait: https://www.etlcpp.com/smallest.html
3536
template<int Bits>
3637
using least_uint = typename modm::uint_t<Bits>::least;
3738

@@ -68,5 +69,25 @@ struct fits_any {
6869
template <typename ... Ts>
6970
using fits_any_t = typename fits_any<Ts...>::type;
7071

72+
/// @ingroup modm_math_utils
73+
74+
/// Generates a bitmask with N bits set
75+
/// @author Thomas Sommer
76+
template<unsigned int N>
77+
struct bitmask {
78+
using value_type = uint_t<N>::least;
79+
static constexpr value_type value = std::pow(2, N) - 1;
80+
81+
constexpr operator value_type() const noexcept
82+
{
83+
return value;
84+
}
85+
};
86+
87+
template<unsigned int N>
88+
using bitmask_t = typename bitmask<N>::value_type;
89+
90+
template<unsigned int N>
91+
static constexpr bitmask_t<N> bitmask_v = bitmask<N>::value;
7192
/// @}
72-
}
93+
} // namespace modm

0 commit comments

Comments
 (0)