Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Bits.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Memory_Bits_h_
5#define _Stroika_Foundation_Memory_Bits_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <memory>
10
11#include "Stroika/Foundation/Common/Common.h"
12
13/**
14 * \file
15 */
16
17namespace Stroika::Foundation::Memory {
18
19 /**
20 * bitNumber's start with 0, not 1.
21 *
22 * \par Example Usage
23 * \code
24 * EXPECT_EQ (Bit (0), 0x1u);
25 * EXPECT_EQ (Bit (1), 0x2u);
26 * EXPECT_EQ (Bit (3), 0x8u);
27 * EXPECT_EQ (Bit (15), 0x8000u);
28 * EXPECT_EQ (Bit<int> (1, 2), 6);
29 * EXPECT_EQ (Bit<int> (1, 2, 15), 0x8006);
30 * \endcode
31 */
32 template <integral INT_TYPE = unsigned int>
33 constexpr INT_TYPE Bit (unsigned int bitNumber);
34 template <integral INT_TYPE, integral... BIT_ARGS>
35 constexpr INT_TYPE Bit (unsigned int bitNumber, const BIT_ARGS&... args);
36
37 /**
38 * Capture the bits from 'bitField' - starting at bit 'startOffset' (zero-based),
39 * extending to endOffset (also zero based - not inclusive). The number of bits captured
40 * is endOffset-startOffset, so:
41 * \pre startOffset <= endOffset
42 *
43 * the result is zero-filled with bits, so if zero bits are captured, the return value will be zero.
44 *
45 * \par Example Usage
46 * \code
47 * EXPECT_EQ (BitSubstring (0x3, 0, 1), 1);
48 * EXPECT_EQ (BitSubstring (0x3, 1, 2), 1);
49 * EXPECT_EQ (BitSubstring (0x3, 2, 3), 0);
50 * EXPECT_EQ (BitSubstring (0x3, 0, 3), 0x3);
51 * EXPECT_EQ (BitSubstring (0xff, 0, 8), 0xff);
52 * EXPECT_EQ (BitSubstring (0xff, 8, 16), 0x0);
53 * EXPECT_EQ (BitSubstring (0b10101010, 0, 1), 0x0); // low# bit on right
54 * EXPECT_EQ (BitSubstring (0b10101010, 7, 8), 0x1); // high# bit on left
55 * \endcode
56 *
57 * \note The startOffset/endOffset pattern matches that with STL iterators (not including the last item)
58 *
59 * \note This was previously named TakeNBitsFrom()
60 */
61 template <integral INT_TYPE>
62 constexpr INT_TYPE BitSubstring (INT_TYPE bitField, unsigned int startOffset, unsigned int endOffset);
63
64}
65
66/*
67 ********************************************************************************
68 ***************************** Implementation Details ***************************
69 ********************************************************************************
70 */
71#include "Bits.inl"
72
73#endif /*_Stroika_Foundation_Memory_Bits_h_*/
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