Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Bits.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include <climits>
5
7
8namespace Stroika::Foundation::Memory {
9
10 /*
11 ********************************************************************************
12 *************************************** Bit ************************************
13 ********************************************************************************
14 */
15 template <integral INT_TYPE>
16 inline constexpr INT_TYPE Bit (unsigned int bitNumber)
17 {
18 Require (bitNumber < CHAR_BIT * sizeof (INT_TYPE));
19 return static_cast<INT_TYPE> (1) << bitNumber;
20 }
21 template <integral INT_TYPE, integral... BIT_ARGS>
22 inline constexpr INT_TYPE Bit (unsigned int bitNumber, const BIT_ARGS&... args)
23 {
24 return Bit<INT_TYPE> (bitNumber) | Bit<INT_TYPE> (args...);
25 }
26
27 /*
28 ********************************************************************************
29 ********************************* BitSubstring *********************************
30 ********************************************************************************
31 */
32 template <integral INT_TYPE>
33 inline constexpr INT_TYPE BitSubstring (INT_TYPE bitField, unsigned int startOffset, unsigned int endOffset)
34 {
35 Require (startOffset <= endOffset);
36 Require (startOffset <= (CHAR_BIT * sizeof (INT_TYPE)));
37 if (startOffset == endOffset) {
38 return 0;
39 }
40 Require (((endOffset - startOffset) - 1 + startOffset) < (CHAR_BIT * sizeof (INT_TYPE)));
41 return (bitField >> startOffset) & ((1 << (endOffset - startOffset)) - 1);
42 }
43
44}
constexpr INT_TYPE Bit(unsigned int bitNumber)
Definition Bits.inl:16
constexpr INT_TYPE BitSubstring(INT_TYPE bitField, unsigned int startOffset, unsigned int endOffset)
Definition Bits.inl:33