Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
qx-bitarray.h
1#ifndef QX_BITARRAY_H
2#define QX_BITARRAY_H
3
4// Shared Lib Support
5#include "qx/core/qx_core_export.h"
6
7// Qt Includes
8#include <QBitArray>
9
10namespace Qx
11{
12
13class QX_CORE_EXPORT BitArray : public QBitArray
14{
15//-Constructor--------------------------------------------------------------------------------------------------
16public:
17 BitArray();
18 BitArray(int size, bool value = false);
19
20//-Class Functions----------------------------------------------------------------------------------------------
21public:
22 template<typename T>
23 requires std::integral<T>
24 static BitArray fromInteger(const T& integer)
25 {
26 int bitCount = sizeof(T)*8;
27
28 BitArray bitRep(bitCount);
29
30 for(int i = 0; i < bitCount; ++i)
31 if(integer & 0b1 << i)
32 bitRep.setBit(i);
33
34 return bitRep;
35 }
36
37//-Instance Functions-------------------------------------------------------------------------------------------
38public:
39 template<typename T>
40 requires std::integral<T>
42 {
43 int bitCount = sizeof(T)*8;
44 T integer = 0;
45
46 for(int i = 0; i < bitCount && i < count(); ++i)
47 integer |= (testBit(i) ? 0b1 : 0b0) << i;
48
49 return integer;
50 }
51
52 QByteArray toByteArray(QSysInfo::Endian endianness = QSysInfo::BigEndian);
53
54 void append(bool bit = false);
55 void replace(const BitArray& bits, int start = 0, int length = -1);
56
57 template<typename T>
58 requires std::integral<T>
59 void replace(T integer, int start = 0, int length = -1)
60 {
61 BitArray converted = BitArray::fromInteger(integer);
62 replace(converted, start, length);
63 }
64
65 BitArray subArray(int start, int length = -1);
66 BitArray takeFromStart(int length = -1);
67 BitArray takeFromEnd(int length = -1);
68
69 BitArray operator<<(int n);
70 void operator<<=(int n);
71 BitArray operator>>(int n);
72 void operator>>=(int n);
73 BitArray operator+(BitArray rhs);
74 void operator+=(const BitArray& rhs);
75};
76
77
78}
79
80#endif // QX_BITARRAY_H
The BitArray class is a more robust variant of QBitArray, which provides an array of bits.
Definition qx-bitarray.h:14
void replace(T integer, int start=0, int length=-1)
Definition qx-bitarray.h:59
T toInteger()
Definition qx-bitarray.h:41
static BitArray fromInteger(const T &integer)
Definition qx-bitarray.h:24
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5
T length(T start, T end)
Definition qx-algorithm.h:29
void setBit(qsizetype i)