?
Solved

bit_vector operation

Posted on 2005-03-11
7
Medium Priority
?
322 Views
Last Modified: 2013-12-14
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
Comment
Question by:sutejok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13521649
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
 
LVL 86

Expert Comment

by:jkr
ID: 13521677
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13521684
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Expert Comment

by:stefan73
ID: 13524410
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
 
LVL 12

Expert Comment

by:stefan73
ID: 13524417
...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 Comment

by:sutejok
ID: 13526134
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
 
LVL 86

Expert Comment

by:jkr
ID: 13527171
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question