tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
test_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_TEST_BIT_HPP
5#define TETL_BIT_TEST_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/// Test 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 test_bit(UInt word, UInt pos) noexcept -> bool
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)) != UInt(0);
24}
25
26/// Test 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 test_bit(UInt word) noexcept -> bool
32{
33 static_assert(Pos < etl::numeric_limits<UInt>::digits);
34 return etl::test_bit(word, static_cast<UInt>(Pos));
35}
36
37} // namespace etl
38
39#endif // TETL_BIT_TEST_BIT_HPP
constexpr auto test_bit(UInt word) noexcept -> bool
Test bit at position Pos
Definition test_bit.hpp:31
constexpr auto test_bit(UInt word, UInt pos) noexcept -> bool
Test bit at position pos.
Definition test_bit.hpp:20
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18