Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 326

# bit_vector operation

How do i reverse all the bits of a bit_vector variable?
0001100 -> 1110011

also, is bit_vector the same as vector<bool> ?

thxx
0
sutejok
• 4
• 2
1 Solution

Commented:
See e.g http://www.sgi.com/tech/stl/bit_vector.html - it states "A bit_vector is essentially a vector<bool>". To  reserve it, you could

bit_vector V(5);
V[0] = true;
V[1] = false;
V[2] = false;
V[3] = true;
V[4] = false;

bit_vector V_reverse(5);

for (bit_vector::reverse_iterator i = V.rbegin(); i < V.rend(); ++i) V_reverse.push_back(*i);

or

void reverse_bit_vector ( bit_vector& in, bit_vector& out) {

for (bit_vector::reverse_iterator i = in.rbegin(); i < in.rend(); ++i) out.push_back(*i);
}
0

Commented:
Oops, sorry, you mean the complement - that can be done like

bit_vector V(5);
V[0] = true;
V[1] = false;
V[2] = false;
V[3] = true;
V[4] = false;

bit_vector V_reverse(5);

for (bit_vector::reverse_iterator i = V.rbegin(); i < V.rend(); ++i) V_reverse.push_back(*i ? false : true);

or

void complement_bit_vector ( bit_vector& in, bit_vector& out) {

for (bit_vector::reverse_iterator i = in.rbegin(); i < in.rend(); ++i) out.push_back(*i ? false : true);
}
0

Commented:
Argh - sorry, correction, the last post should be

bit_vector V(5);
V[0] = true;
V[1] = false;
V[2] = false;
V[3] = true;
V[4] = false;

bit_vector V_reverse(5);

for (bit_vector::iterator i = V.begin(); i < V.end(); ++i) V_reverse.push_back(*i ? false : true);

or

void complement_bit_vector ( bit_vector& in, bit_vector& out) {

for (bit_vector::iterator i = in.begin(); i < in.end(); ++i) out.push_back(*i ? false : true);
}
0

Commented:
Hi jkr,
> for (bit_vector::iterator i = in.begin(); i < in.end(); ++i) out.push_back(*i
> ? false : true);
Hmmm, why don't you use:

for (bit_vector::iterator i = in.begin(); i < in.end(); ++i) out.push_back(~ *i );

Also, a modification in place might be helpful, depending on your needs:

for (bit_vector::iterator i = in.begin(); i < in.end(); ++i) *i ^= true;

Cheers!

Stefan
0

Commented:
...but it would be a lot more efficient to modify the underlying base types (probably a vector of 32 bit integers). According to SGI's documentation, bit_vector is optimized for space, not performance.
0

Author Commented:
thx for the answer guys.. I was actually hoping that there is an operation defined in bitvector to just invert all the bits. something like : ~(bitv_var)

one more short one if you dont mind: is there a way to "AND" two bitvectors? i mean like (bitv_var1)&(bitv_var2) ? without iteration..
0

Commented:
No, unfortunately, a 'but_vector()' has no conversion operators and no other operator that could handle this. You might be better off with a 'std::bitset', which has 'to_ulong()', which could serve that purpose.
0

## Featured Post

• 4
• 2
Tackle projects and never again get stuck behind a technical roadblock.