stellarlib 0.1.0
Loading...
Searching...
No Matches
bit.hpp
1/* clang-format off */
2
3/*
4 stellarlib
5 Copyright (C) 2025-2026 Domán Zana
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#ifndef STELLARLIB_EXT_BIT_HPP
25#define STELLARLIB_EXT_BIT_HPP
26
27#include <limits>
28
29/**
30 * @brief Standard library extensions
31 */
33{
34/**
35 * @brief Calculates the segment index of a bit in an array
36 * @tparam T Unsigned integral type of the segment
37 * @param arg Index of the bit
38 * @return Segment index of the bit in the array
39 */
40template <typename T>
41[[nodiscard]]
42constexpr auto bit_index(const T arg) noexcept
43{
44 return static_cast<T>(arg / std::numeric_limits<T>::digits);
45}
46
47/**
48 * @brief Calculates the segment mask of a bit in an array
49 * @tparam T Unsigned integral type of the segment
50 * @param arg Index of the bit
51 * @return Segment mask of the bit in the array
52 */
53template <typename T>
54[[nodiscard]]
55constexpr auto bit_mask(const T arg) noexcept
56{
57 return static_cast<T>(T{1} << static_cast<T>(arg % std::numeric_limits<T>::digits));
58}
59}
60
61#endif
Standard library extensions.
Definition bit.hpp:33
constexpr auto bit_mask(const T arg) noexcept
Calculates the segment mask of a bit in an array.
Definition bit.hpp:55
constexpr auto bit_index(const T arg) noexcept
Calculates the segment index of a bit in an array.
Definition bit.hpp:42