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// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_BIT_SET_BIT_HPP
5#define TETL_BIT_SET_BIT_HPP
6
7#include <etl/_concepts/builtin_unsigned_integer.hpp>
8#include <etl/_contracts/check.hpp>
9#include <etl/_cstddef/size_t.hpp>
10#include <etl/_limits/numeric_limits.hpp>
11
12namespace etl {
13
14/// Set bit at position \p pos
15/// \details https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit
16/// \pre Position \p pos must be a valid bit-index for UInt
17/// \note Non-standard extension
18/// \ingroup bit
19template <etl::builtin_unsigned_integer UInt>
20[[nodiscard]] constexpr auto set_bit(UInt word, UInt pos) noexcept -> UInt
21{
22 TETL_PRECONDITION(static_cast<int>(pos) < etl::numeric_limits<UInt>::digits);
23 return static_cast<UInt>(word | static_cast<UInt>(UInt(1) << pos));
24}
25
26/// Set bit at position \p pos to \p value
27/// \details https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit
28/// \pre Position \p pos must be a valid bit-index for UInt
29/// \note Non-standard extension
30/// \ingroup bit
31template <etl::builtin_unsigned_integer UInt>
32[[nodiscard]] constexpr auto set_bit(UInt word, UInt pos, bool value) -> UInt
33{
34 TETL_PRECONDITION(static_cast<int>(pos) < etl::numeric_limits<UInt>::digits);
35 return static_cast<UInt>((word & static_cast<UInt>(~(UInt(1) << pos))) | (UInt(value) << pos));
36}
37
38/// Set bit at position `Pos`
39/// \details https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit
40/// \note Non-standard extension
41/// \ingroup bit
42template <etl::size_t Pos, etl::builtin_unsigned_integer UInt>
43[[nodiscard]] constexpr auto set_bit(UInt word) noexcept -> UInt
44{
45 static_assert(Pos < etl::numeric_limits<UInt>::digits);
46 return etl::set_bit(word, static_cast<UInt>(Pos));
47}
48
49/// Set bit at position `Pos` to \p value
50/// \details https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit
51/// \note Non-standard extension
52/// \ingroup bit
53template <etl::size_t Pos, etl::builtin_unsigned_integer UInt>
54[[nodiscard]] constexpr auto set_bit(UInt word, bool value) noexcept -> UInt
55{
56 static_assert(Pos < etl::numeric_limits<UInt>::digits);
57 return etl::set_bit(word, static_cast<UInt>(Pos), value);
58}
59
60} // namespace etl
61
62#endif // TETL_BIT_SET_BIT_HPP
constexpr auto set_bit(UInt word, UInt pos) noexcept -> UInt
Set bit at position pos.
Definition set_bit.hpp:20
constexpr auto set_bit(UInt word, UInt pos, bool value) -> UInt
Set bit at position pos to value.
Definition set_bit.hpp:32
constexpr auto set_bit(UInt word) noexcept -> UInt
Set bit at position Pos
Definition set_bit.hpp:43
constexpr auto set_bit(UInt word, bool value) noexcept -> UInt
Set bit at position Pos to value.
Definition set_bit.hpp:54
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18