1 /***************************************************************************
2 * Copyright (C) 2005 by Joris Guisson *
3 * joris.guisson@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
26 BitSet::BitSet(quint32 num_bits
) : num_bits(num_bits
),data(0)
28 num_bytes
= (num_bits
/ 8) + ((num_bits
% 8 > 0) ? 1 : 0);
29 data
= new quint8
[num_bytes
];
30 std::fill(data
,data
+num_bytes
,0x00);
34 BitSet::BitSet(const quint8
* d
,quint32 num_bits
) : num_bits(num_bits
),data(0)
36 num_bytes
= (num_bits
/ 8) + ((num_bits
% 8 > 0) ? 1 : 0);
37 data
= new quint8
[num_bytes
];
38 memcpy(data
,d
,num_bytes
);
49 BitSet::BitSet(const BitSet
& bs
) : num_bits(bs
.num_bits
),num_bytes(bs
.num_bytes
),data(0),num_on(bs
.num_on
)
51 data
= new quint8
[num_bytes
];
52 std::copy(bs
.data
,bs
.data
+num_bytes
,data
);
62 BitSet
& BitSet::operator = (const BitSet
& bs
)
66 num_bytes
= bs
.num_bytes
;
67 num_bits
= bs
.num_bits
;
68 data
= new quint8
[num_bytes
];
69 std::copy(bs
.data
,bs
.data
+num_bytes
,data
);
74 void BitSet::setAll(bool on
)
76 std::fill(data
,data
+num_bytes
,on
? 0xFF : 0x00);
77 num_on
= on
? num_bits
: 0;
85 void BitSet::orBitSet(const BitSet
& other
)
90 bool val
= get(i
) || other
.get(i
);
96 bool BitSet::allOn() const
98 return num_on
== num_bits
;
101 bool BitSet::operator == (const BitSet
& bs
)
103 if (this->getNumBits() != bs
.getNumBits())
106 return memcmp(data
,bs
.data
,num_bytes
) == 0;