**Module:** Essential Tools Module **Group:** Traditional Collection

Does not inherit

- Local Index
- Synopsis
- Description
- Persistence
- Example
- Public Constructors
- Public Destructor
- Assignment Operators
- Indexing Operators
- Logical Operators
- Public Member Functions
- Related Global Functions

#include <rw/bitvec.h> RWBitVec v;

Class ** RWBitVec** is a bit vector whose length can be changed at run time. Because this requires an extra level of indirection, this makes it slightly less efficient than classes

Simple

#include <rw/bitvec.h> #include <rw/rstream.h> int main(){ // Allocate a vector with 20 bits, set to TRUE: RWBitVec av(20, TRUE); av(0) = FALSE; // Turn bit 0 off av(2) = FALSE; // Turn bit 2 off av.clearBit(7); // Turn bit 7 off av.setBit(2); // Turn bit 2 back on for(int i=11; i<=14; i++) av(i) = FALSE; std::cout << av << std::endl; // Print the vector out return 0; }

*Program output:*

[ 0 1 1 1 1 1 1 0 1 1 1 0 0 0 0 1 1 1 1 1 ]

RWBitVec();

Construct a zero length (null) vector.

RWBitVec(size_t N);

Construct a vector with `N` bits. The initial value of the bits is undefined.

RWBitVec(size_t N, RWBoolean initVal);

Construct a vector with `N` bits, each set to the Boolean value `initVal`.

RWBitVec(const RWByte* bp, size_t N);

Construct a vector with `N` bits, initialized to the data in the array of bytes pointed to by `bp`. This array must be at least long enough to contain `N` bits. The identifier `RWByte` is a typedef for an `unsigned char`.

RWBitVec(const RWBitVec& v);

Copy constructor. Uses value semantics -- the constructed vector will be a copy of `v`.

~RWBitVec();

The destructor. Releases any allocated memory.

RWBitVec&operator=(const RWBitVec& v);

Assignment operator. Value semantics are used -- self will be a copy of `v`.

RWBitVec&operator=(RWBoolean b);

Assignment operator. Sets every bit in self to the boolean value `b`.

RWBitVec&operator&=(const RWBitVec& v); RWBitVec&operator^=(const RWBitVec& v); RWBitVec&operator|=(const RWBitVec& v);

Logical assignments. Set each element of self to the logical AND, XOR, or OR, respectively, of self and the corresponding bit in `v`. Self and `v` must have the same number of elements (i.e., be conformal) or an exception of type ** RWInternalErr** will occur.

RWBitRefoperator[](size_t i);

Returns a reference to bit `i` of self. A helper class, ** RWBitRef**, is used. The result can be used as an lvalue. The index

RWBitRefoperator()(size_t i);

Returns a reference to bit `i` of self. A helper class, ** RWBitRef**, is used. The result can be used as an lvalue. The index

RWBooleanoperator[](size_t i) const;

Returns the boolean value of bit `i`. The result cannot be used as an lvalue. The index `i` must be between `0` and the length of the vector less one. Bounds checking is performed. If the index is out of range, then an exception of type ** RWBoundsErr** will occur.

RWBooleanoperator()(size_t i) const;

Returns the boolean value of bit `i`. The result cannot be used as an lvalue. The index `i` must be between `0` and the length of the vector less one. Bounds checking is performed only if the preprocessor macro `RWBOUNDS_CHECK` has been defined before including the header file `<rw/bitvec.h>`. If so, and if the index is out of range, then an exception of type ** RWBoundsErr** will occur.

RWBooleanoperator==(const RWBitVec& u) const;

Returns `TRUE` if self and `v` have the same length and if each bit of self is set to the same value as the corresponding bit in `v`. Otherwise, returns `FALSE`.

RWBooleanoperator!=(const RWBitVec& u) const;

Returns `FALSE` if self and `v` have the same length and if each bit of self is set to the same value as the corresponding bit in `v`. Otherwise, returns `TRUE`.

RWBooleanoperator==(RWBoolean b) const;

Returns `TRUE` if every bit of self is set to the boolean value `b`. Otherwise `FALSE`.

RWBooleanoperator!=(RWBoolean b) const;

Returns `FALSE` if every bit of self is set to the boolean value `b`. Otherwise `TRUE`.

voidclearBit(size_t i);

Clears (i.e., sets to `FALSE`) the bit with index `i`. The index `i` must be between `0` and the length of the vector less one. No bounds checking is performed. The following are equivalent, although `clearBit(size_t)` is slightly smaller and faster than using `operator()(size_t)`:

a(i) = FALSE; a.clearBit(i);

const RWByte*data() const;

Returns a `const` pointer to the raw data of self. Should be used with care.

size_tfirstFalse() const;

Returns the index of the first `FALSE` bit in self. Returns `RW_NPOS` if there is no `FALSE` bit.

size_tfirstTrue() const;

Returns the index of the first `TRUE` bit in self. Returns `RW_NPOS` if there is no `TRUE` bit.

unsignedhash() const;

Returns a value suitable for hashing.

RWBooleanisEqual(const RWBitVec& v) const;

Returns `TRUE` if self and `v` have the same length and if each bit of self is set to the same value as the corresponding bit in `v`. Otherwise, returns `FALSE`.

size_tlength() const;

Returns the number of bits in the vector.

ostream&printOn(ostream& s) const;

Print the vector `v` on the output stream `s`. See the example above for a sample of the format.

voidresize(size_t N);

Resizes the vector to have length `N`. If this results in a lengthening of the vector, the additional bits will be set to `FALSE`.

istream&scanFrom(istream&);

Read the bit vector from the input stream `s`. The vector will dynamically be resized as necessary. The vector should be in the same format printed by member function `printOn(ostream&)`.

voidsetBit(size_t i);

Sets (i.e., sets to `TRUE`) the bit with index `i`. The index `i` must be between `0` and `size-1`. No bounds checking is performed. The following are equivalent, although `setBit(size_t)` is slightly smaller and faster than using `operator()(size_t)`:

a(i) = TRUE; a.setBit(i);

size_tsum(const RWBitVec& v);

Returns the total number of bits set in the vector `v`.

RWBooleantestBit(size_t i) const;

Tests the bit with index `i`. The index `i` must be between `0` and `size-1`. No bounds checking is performed. The following are equivalent, although `testBit(size_t)` is slightly smaller and faster than using `operator()(size_t)`:

if( a(i) ) doSomething(); if( a.testBit(i) ) doSomething();

RWBitVecoperator!(const RWBitVec& v);

Unary operator that returns the logical negation of vector `v`.

RWBitVecoperator&(const RWBitVec&,const RWBitVec&); RWBitVecoperator^(const RWBitVec&,const RWBitVec&); RWBitVecoperator|(const RWBitVec&,const RWBitVec&);

Returns a vector that is the logical `AND`, `XOR`, or `OR` of the vectors `v1` and `v2`. The two vectors must have the same length or an exception of type `RWInternalErr` will occur.

ostream&operator<<(ostream& s, const RWBitVec& v);

Calls `v.printOn(s)`.

istream&operator>>(istream& s, RWBitVec& v);

Calls `v.scanFrom(s)`.

RWvostream&operator<<(RWvostream&, const RWBitVec& vec); RWFile&operator<<(RWFile&, const RWBitVec& vec);

Saves the *RWBitVec*`vec` to a virtual stream or ** RWFile**, respectively.

RWvistream&operator>>(RWvistream&, RWBitVec& vec); RWFile&operator>>(RWFile&, RWBitVec& vec);

Restores an ** RWBitVec** into

©2004 Copyright Quovadx, Inc. All Rights Reserved.

Rogue Wave and SourcePro are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.

Contact Rogue Wave about documentation or support issues.