tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
set_bit.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_BIT_SET_BIT_HPP
4#define TETL_BIT_SET_BIT_HPP
5
10
11namespace etl {
12
18template <etl::builtin_unsigned_integer UInt>
19[[nodiscard]] constexpr auto set_bit(UInt word, UInt pos) noexcept -> UInt
20{
22 return static_cast<UInt>(word | static_cast<UInt>(UInt(1) << pos));
23}
24
30template <etl::builtin_unsigned_integer UInt>
31[[nodiscard]] constexpr auto set_bit(UInt word, UInt pos, bool value) -> UInt
32{
34 return static_cast<UInt>((word & static_cast<UInt>(~(UInt(1) << pos))) | (UInt(value) << pos));
35}
36
41template <etl::size_t Pos, etl::builtin_unsigned_integer UInt>
42[[nodiscard]] constexpr auto set_bit(UInt word) noexcept -> UInt
43{
44 static_assert(Pos < etl::numeric_limits<UInt>::digits);
45 return etl::set_bit(word, static_cast<UInt>(Pos));
46}
47
52template <etl::size_t Pos, etl::builtin_unsigned_integer UInt>
53[[nodiscard]] constexpr auto set_bit(UInt word, bool value) noexcept -> UInt
54{
55 static_assert(Pos < etl::numeric_limits<UInt>::digits);
56 return etl::set_bit(word, static_cast<UInt>(Pos), value);
57}
58
59} // namespace etl
60
61#endif // TETL_BIT_SET_BIT_HPP
#define TETL_PRECONDITION(...)
Definition check.hpp:16
constexpr auto set_bit(UInt word, UInt pos) noexcept -> UInt
Set bit at position pos.
Definition set_bit.hpp:19
Definition adjacent_find.hpp:8
static constexpr int digits
Definition numeric_limits.hpp:24