/
BitStringNew.h
96 lines (72 loc) · 2.23 KB
/
BitStringNew.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// BitString.h
//
// Uses 0-based indexing
#ifndef _VBI_POLYMATH_N_BITSTRING_H_
#define _VBI_POLYMATH_N_BITSTRING_H_
// TO DO
// typedef std::vector<ULONG> WordArray;
// typedef std::vector<ULONG>::iterator WordArrayIter;
extern USHORT BitsPerWord;
// NumOnes[k] contains the number of 1's in the binary representation of the integer k
extern const int NumOnes[];
// Count the number of 1's in a single ULONG
size_t BitCount( ULONG x );
class BitString
{
public:
// Default constructor ( all zeros bitstring of num_bits )
BitString( size_t num_bits )
{
mN = num_bits;
if( num_bits > 0 )
{
// Compute the number of words we need to hold mN bits
USHORT k = (USHORT) ( 1 + ( (num_bits - 1) / BitsPerWord ));
// Insert k words of value 0
mWords.assign( k, 0 );
}
}
// Copy Contruct from another BitString
BitString( const BitString& other )
: mWords( other.mWords ), mN( other.mN )
{
}
// Return true if bit number i = 1 (can not be used for assignment)
bool operator[]( size_t i ) const;
// Bitwise AND operator (Performs * in F_2^n)
BitString operator&( const BitString& other ) const;
// Bitwise XOR operator (Performs + in F_2^n)
BitString operator^( const BitString& other ) const;
// Bitwise OR operator
BitString operator|( const BitString& other ) const;
// Reset all bits of this bitstring
inline void Reset()
{
mWords.clear();
// Compute the number of words we need to hold mN bits
size_t k = 1 + ( (mN - 1) / BitsPerWord );
mWords.assign( k, 0 );
}
// Equality test operator
bool operator==( const BitString& other ) const;
// Count the number of 1 bits in this BitString
size_t Count( ) const;
// Return the number of bits in this BitString
inline size_t Size() const
{
return mN;
}
// Set - set the b'th bit of this term
void Set( size_t b );
// Reset - reset the b'th bit of this term
void Reset( size_t b );
// Flip the b'th bit of this term
void Flip( size_t b );
// Support output of a BitString to a stream
friend std::ostream& operator<<( std::ostream& out, const BitString& t );
private:
// Number of bits
size_t mN;
WordArray mWords;
};
#endif // _VBI_POLYMATH_N_BITSTRING_H_