/* """
i did some research on the bit_vectorhttp://
www.sgi.com/tech/stl/bit_vector.html
and it seems to me that it best not to use it?
""" */
If you made that decision because you saw the 'deprecated' in the docs, that
was 7 years ago and it's still in all/most implementations, AFAIK. But, the
decision is yours.
/* """
anyway ive done the next sted of my code and made an adder:
@code
std::vector<bool> Add(std::vector<bool>B1, std::vector<bool>B2) {
std::vector<bool>a;
// -----
for(unsigned long i = 1; i > 0
{ // im not sure if this is nessacery.
if(B1.size() > B2.size())
B2.push_back(0);
else if(B1.size() < B2.size())
B1.push_back(0);
else
i = 0;
}
// -----
""" */
Looks like you want to make the two vectors the same size.
Another way:
{ using std::cout // for NG post
std::vector<bool> vecA(5, true);
std::vector<bool> vecB;
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
while( vecA.size() > vecB.size()){ vecB.push_back(0);}
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
while( vecA.size() < vecB.size()){ vecA.push_back(0);}
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
// - reverse the sizes -
std::vector<bool> vecC;
std::vector<bool> vecD(5, true);
while( vecC.size() > vecD.size()){ vecD.push_back(0);}
while( vecC.size() < vecD.size()){ vecC.push_back(0);}
cout<<"vecC.size()="<<vecC.size()
<<" vecD.size()="<<vecD.size()<<std::endl;
/* - output -
vecA.size()=5 vecB.size()=0
vecA.size()=5 vecB.size()=5
vecA.size()=5 vecB.size()=5
vecC.size()=5 vecD.size()=5
*/
}
Only one of the two while() loops (I showed two examples above) will
execute, the other will 'fall through'. If the vectors are the same size,
*both* while() loop conditions will be false, and thus 'fall through'(not
execute).
The *two* while()s, one after the other, look a little strange, but better
than a chain of if()/else, and it pretty much is self commenting code (any
programmer can easily see what it's doing.). Try it, I think you'll like it.
<G>
/* """
unsigned long carry = 0;
for(unsigned long i = 0; i < B1.size(); i++) { // i am sure this can
be done better.
a.push_back(((B1
^ B2) ^ carry) | ((B1 & B2) &
carry));
carry = (B1 & B2) | (B1 & carry) | (B2 & carry);
}
if(carry == 1)
a.push_back(carry);
return a;}
@code
""" */
If only you could use a 'bitset', it would be TOO easy. <G>
Another thing to consider, you pass the two vectors 'by value'. That means a
copy of each (not too much concern if the size is small). I'd pass them by
'const reference':
std::vector<bool> Add( std::vector<bool>const &B1,
std::vector<bool>const &B2) { /* .... */ }
*BUT*, that won't work because you need to change the vectors inside the
function. You would need to adjust the size before passing them to Add().
Just thought I'd mention that as it's good to think about these things when
designing a class (not as an after thought).
If it needs to be done more than once, I'd make a separate 'private:' helper
function to adjust the sizes:
// in class
private:
bool AdjustSize( std::vector<bool> &B1,
std::vector<bool> &B2 ) /*const*/{
if( B1.empty() && B2.empty() ){
return false; // failure, both empty
} // if()
while( B1.size() > B2.size() ){ B2.push_back(0);}
while( B1.size() < B2.size() ){ B1.push_back(0);}
return true;
} // AdjustSize( vector&, vector&)
std::vector<bool> Add( std::vector<bool> B1,
std::vector<bool> B2){
if( not AdjustSize( B1, B2) ){ throw std::terminate();} // or exit(1);
/* .... */
} // Add(vector, vector)
I'm still a little 'foggy' as to what you are trying to solve overall
exactly. <G>
Is this homework, book excersize, hobby project, <choke>work?
[ corrections, as always, are welcome. ]