tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
reset_bit.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3
4#ifndef TETL_BIT_RESET_BIT_HPP
5#define TETL_BIT_RESET_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/// Reset 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 reset_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/// Reset bit at position Pos
27/// \details https://stackoverflow.com/questions/47981/how-to-set-clear-and-toggle-a-single-bit
28/// \note Non-standard extension
29/// \ingroup bit
30template <etl::size_t Pos, etl::builtin_unsigned_integer UInt>
31[[nodiscard]] constexpr auto reset_bit(UInt word) noexcept -> UInt
32{
33 static_assert(Pos < etl::numeric_limits<UInt>::digits);
34 return etl::reset_bit(word, static_cast<UInt>(Pos));
35}
36
37} // namespace etl
38
39#endif // TETL_BIT_RESET_BIT_HPP
constexpr auto reset_bit(UInt word) noexcept -> UInt
Reset bit at position Pos.
Definition reset_bit.hpp:31
constexpr auto reset_bit(UInt word, UInt pos) noexcept -> UInt
Reset bit at position pos.
Definition reset_bit.hpp:20
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18