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
  • Last Modified:

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
Asked:
sutejok
  • 4
  • 2
1 Solution
 
jkrCommented:
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
 
jkrCommented:
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
 
jkrCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
stefan73Commented:
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
 
stefan73Commented:
...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
 
sutejokAuthor 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
 
jkrCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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