cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TBitField.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26#include <cassert>
27
28namespace cppcore {
29
30//-------------------------------------------------------------------------------------------------
35//-------------------------------------------------------------------------------------------------
36template <class T>
37class TBitField {
38public:
40 TBitField() = default;
41
44 explicit TBitField(T init);
45
47 ~TBitField() = default;
48
51 T getMask() const;
52
56 bool constexpr getBit(size_t pos) const noexcept ;
57
61 void setBit(size_t pos, bool on);
62
65 void setBit(size_t pos);
66
69 void clearBit(size_t pos);
70
72 void clear();
73
76 size_t maxBits() const;
77
78private:
79 T mBitMask{0};
80};
81
82template <class T>
83inline TBitField<T>::TBitField(T init) : mBitMask(init) {
84 // empty
85}
86
87template <class T>
88inline T TBitField<T>::getMask() const {
89 return mBitMask;
90}
91
92template <class T>
93inline bool constexpr TBitField<T>::getBit(size_t pos) const noexcept {
94 assert(pos < maxBits());
95 return (mBitMask & (1 << pos)) != 0;
96}
97
98template <class T>
99inline void TBitField<T>::setBit(size_t pos, bool on) {
100 assert(pos < maxBits());
101 if (on) {
102 setBit(pos);
103 } else {
104 clearBit(pos);
105 }
106}
107
108template <class T>
109inline void TBitField<T>::setBit(size_t pos) {
110 assert(pos < maxBits());
111 mBitMask = mBitMask | 1 << pos;
112}
113
114template <class T>
115inline void TBitField<T>::clearBit(size_t pos) {
116 assert(pos < maxBits());
117 mBitMask = mBitMask & ~(1 << pos);
118}
119
120template <class T>
121inline void TBitField<T>::clear() {
122 mBitMask = 0;
123}
124
125template <class T>
126inline size_t TBitField<T>::maxBits() const {
127 constexpr size_t numBits = sizeof(T) * 8;
128 return numBits;
129}
130
131} // namespace cppcore
size_t maxBits() const
Returns the upper limit for bit positions.
Definition TBitField.h:126
void clearBit(size_t pos)
Will clear the bit at the given postion to false.
Definition TBitField.h:115
T getMask() const
Returns the current bit-mask.
Definition TBitField.h:88
void setBit(size_t pos, bool on)
Will set the bit at the given position to the given state.
Definition TBitField.h:99
TBitField()=default
The default class constructor.
bool constexpr getBit(size_t pos) const noexcept
Will return the bit at the given position.
Definition TBitField.h:93
void clear()
Will clear all bits, they will set to false.
Definition TBitField.h:121
~TBitField()=default
The class destructor.