473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dec2Bin conversion

This is pretty much beginners stuff, but anyway i though i would ask
the question.
Does it have to be this complicated?
I think the below code speaks for itself, but basicly i need to
convert an unsigned integer into binary and stuff it into a boolean
vector. (supose an aray could do allso, but this is easyer)

@code
#include <vector>
#include <iostream>

class T {

std::vector<bool>B;

public:

void Convert(unsigned long a) {
B.clear(); //just in case
for(int i = 0; i < 32; i++, a = a >1) {
B.push_back(a & 1);
}
for(int i = 31; i >= 0 && B[i] != 1; i--) {
B.pop_back();
}
}

void Print() {
for(int i = B.size() - 1; i >= 0; i--) {
std::cout << B[i];
}
}

} test;

int main() {
test.Convert(255);
test.Print();
std::cin.get();
}
@code

Your input will be appreciated.

Regards

May 14 '07 #1
8 4607

<za******@gmail.comwrote in message ...
This is pretty much beginners stuff, but anyway i though i would ask
the question.
Does it have to be this complicated?
I think the below code speaks for itself, but basicly i need to
convert an unsigned integer into binary and stuff it into a boolean
vector. (supose an aray could do allso, but this is easyer)

@code
#include <vector>
#include <iostream>

class T {
std::vector<bool>B;
public:
void Convert(unsigned long a) {
B.clear(); file://just in case
for(int i = 0; i < 32; i++, a = a >1) {
B.push_back(a & 1);
}
for(int i = 31; i >= 0 && B[i] != 1; i--) {
B.pop_back();
}
}

void Print() {
for(int i = B.size() - 1; i >= 0; i--) {
std::cout << B[i];
}
}

} test;

int main() {
test.Convert(255);
test.Print();
std::cin.get();
}
@code

Your input will be appreciated.
Regards
#include <bitset>
int main(){
std::bitset<64b;
b=255;
std::cout<<"b=255;\n"<< b <<std::endl;
return 0;
}

Also check out 'std::bit_vector'.

--
Bob R
POVrookie
May 14 '07 #2
On 14 Maj, 08:02, "BobR" <removeBadB...@worldnet.att.netwrote:
<zacar...@gmail.comwrote in message ...
This is pretty much beginners stuff, but anyway i though i would ask
the question.
Does it have to be this complicated?
I think the below code speaks for itself, but basicly i need to
convert an unsigned integer into binary and stuff it into a boolean
vector. (supose an aray could do allso, but this is easyer)
@code
#include <vector>
#include <iostream>
class T {
std::vector<bool>B;
public:
void Convert(unsigned long a) {
B.clear(); file://just in case
for(int i = 0; i < 32; i++, a = a >1) {
B.push_back(a & 1);
}
for(int i = 31; i >= 0 && B[i] != 1; i--) {
B.pop_back();
}
}
void Print() {
for(int i = B.size() - 1; i >= 0; i--) {
std::cout << B[i];
}
}
} test;
int main() {
test.Convert(255);
test.Print();
std::cin.get();
}
@code
Your input will be appreciated.
Regards

#include <bitset>
int main(){
std::bitset<64b;
b=255;
std::cout<<"b=255;\n"<< b <<std::endl;
return 0;
}

Also check out 'std::bit_vector'.

--
Bob R
POVrookie- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -
bitset is not an option as it is limited to whatever number of bits
you choose to assign. It needs to be dynamicly.

I actually thought that vector<boolwas more or less the same as
bit_vector, except ive heard that i should use vector<bool>

May 14 '07 #3

<za******@gmail.comwrote in message ...
>bitset is not an option as it is limited to whatever number of bits
you choose to assign. It needs to be dynamicly.
I actually thought that vector<boolwas more or less the same as
bit_vector, except ive heard that i should use vector<bool>
[ My STL docs are a little old, so, take this with a grain of salt. <G ]
"A bit_vector is essentially a vector<bool>: it is a Sequence that has the
same interface as vector. The main difference is that bit_vector is
optimized for space efficiency. ** A vector always requires at least one
byte per element, but a bit_vector only requires one bit per element. ** "

So, if you want to save some memory, use the 'std::bit_vector'.

OK, another little nitpik. You use the 'magic numbers' 32 and 31 in your
loops. On your machine an 'int' (or long) may be 32 bits, BUT, the standard
does NOT require that (it could be 16bits or 64bits). You should use 'sizeof
int' to get the size on the machine it is being compiled on.

std::size_t IntSize( sizeof(int) * CHAR_BIT );
cout <<"sizeof(int) = "<<IntSize<<std::endl;
// out: sizeof(int) = 32

I know you asked how to simplify, so being the jerk, I added complexity!
Ain't I a stinker? <G>

Anyway, play with that, then re-post your new code and I (or one of the
experts here) will see if it can be improved.
The only thing I see at this point is a style issue(mine):

void Print() {
for( std::size_t i( B.size()-1 ); i >= 0; --i ){
std::cout << B[i];
// std::cout << B.at( i ); // if you want range check (exception)
} // Print()

If you do it like this, you could output to file, stringstream, etc.:

void Print( std::ostream &out) {
for( std::size_t i( B.size() - 1 ); i >= 0; --i ){
out << B.at( i );
} // Print()

int main(){
test.Convert(255);
test.Print( std::cout );
std::ofstream nuts("test.txt");
test.Print( nuts );
return 0;
}

--
Bob R
POVrookie
May 14 '07 #4
On 14 Maj, 20:47, "BobR" <removeBadB...@worldnet.att.netwrote:
<zacar...@gmail.comwrote in message ...
bitset is not an option as it is limited to whatever number of bits
you choose to assign. It needs to be dynamicly.
I actually thought that vector<boolwas more or less the same as
bit_vector, except ive heard that i should use vector<bool>

[ My STL docs are a little old, so, take this with a grain of salt. <G ]
"A bit_vector is essentially a vector<bool>: it is a Sequence that has the
same interface as vector. The main difference is that bit_vector is
optimized for space efficiency. ** A vector always requires at least one
byte per element, but a bit_vector only requires one bit per element. ** "

So, if you want to save some memory, use the 'std::bit_vector'.

OK, another little nitpik. You use the 'magic numbers' 32 and 31 in your
loops. On your machine an 'int' (or long) may be 32 bits, BUT, the standard
does NOT require that (it could be 16bits or 64bits). You should use 'sizeof
int' to get the size on the machine it is being compiled on.

std::size_t IntSize( sizeof(int) * CHAR_BIT );
cout <<"sizeof(int) = "<<IntSize<<std::endl;
// out: sizeof(int) = 32

I know you asked how to simplify, so being the jerk, I added complexity!
Ain't I a stinker? <G>

Anyway, play with that, then re-post your new code and I (or one of the
experts here) will see if it can be improved.
The only thing I see at this point is a style issue(mine):

void Print() {
for( std::size_t i( B.size()-1 ); i >= 0; --i ){
std::cout << B[i];
// std::cout << B.at( i ); // if you want range check (exception)
} // Print()

If you do it like this, you could output to file, stringstream, etc.:

void Print( std::ostream &out) {
for( std::size_t i( B.size() - 1 ); i >= 0; --i ){
out << B.at( i );
} // Print()

int main(){
test.Convert(255);
test.Print( std::cout );
std::ofstream nuts("test.txt");
test.Print( nuts );
return 0;
}

--
Bob R
POVrookie
Thanks for the feedback, aspecial about the vector, as people keep
telling me that vector<boolonly take up one bit of memory per
element even though it is my experience that it takes up one byte of
memory.
Next i would propably be on its place to mention that any input number
(for now) wont be greater than 32/64 bit, but the out but can be
unlimited. The conversion in it self is not so important, i just dont
want to write a whole lot of code to do it, if there is a simple way.

the code as it looks now (havent played with the bit_vector yet):
@code
class T {
std::vector< std::vector<boolB;
public:
void Clear() {
B.clear();
}
void Conv(unsigned long a) {
std::vector<bool>c;
for(int i = 0; i < 32; i++, a = a >1)
c.push_back(a & 1);
for(int i = 31; i >= 0 && c[i] != 1; i--)
c.pop_back();
B.push_back(c);
}
void Print(unsigned long a) {
if(a < B.size()) {
for(int i = B[a].size() - 1; i >= 0; i--)
std::cout << B[a][i];
}
else
std::cout << "ERROR: element does not exist!";
}
};
@code

If we assume this is as it should be, my next task will be a little
more complicated.
x = input value
2^x is what i need to calculate and of course the result will be so
great that i wont be able to store it in a 32/64 integer and thats the
reason for the weird way of doing thing.
The method im thinking about using is: "left-to-right binary
exponentiation" (http://primes.utm.edu/glossary/page.php?
sort=BinaryExponentiation).

i have some trouble explaining my self in english, but i hope im doing
ok.

May 14 '07 #5

<za******@gmail.comwrote in message ...
On 14 Maj, 20:47, "BobR" <removeBadB...@worldnet.att.netwrote:

Thanks for the feedback, aspecial about the vector, as people keep
telling me that vector<boolonly take up one bit of memory per
element even though it is my experience that it takes up one byte of
memory.
Next i would propably be on its place to mention that any input number
(for now) wont be greater than 32/64 bit, but the out but can be
unlimited. The conversion in it self is not so important, i just dont
want to write a whole lot of code to do it, if there is a simple way.

the code as it looks now (havent played with the bit_vector yet):
@code
class T { [snip, see below ] };
@code

If we assume this is as it should be, my next task will be a little
more complicated.
x = input value
2^x is what i need to calculate and of course the result will be so
great that i wont be able to store it in a 32/64 integer and thats the
reason for the weird way of doing thing.
If you don't know x's size (or can't calculate it), how do you plan to pass
it to the method?
T::Calc(std::bit_vector const &Bvec){/* parse it */} // ??

Maybe you could explain a little more what you are trying to solve.
Explain exactly what you plan to input, and what you want for output.

The method im thinking about using is: "left-to-right binary
exponentiation" (http://primes.utm.edu/glossary/page.php?
sort=BinaryExponentiation).

i have some trouble explaining my self in english, but i hope im doing
ok.
You are doing fine. Even some educated 'native English speaking' people
sometimes have trouble explaining things. <G(not me. I ain't educated!).

I just did a quick test using bit_vector. I changed these:

class T {
// std::vector< std::vector<boolB;
std::vector< std::bit_vector >B; // <--- here
public:
void Clear(){ B.clear();}
void Conv(unsigned long a) {
// std::vector<bool>c;
std::bit_vector c; // <--- here (2)
for(int i = 0; i < 32; i++, a = a >1)
c.push_back(a & 1);
for(int i = 31; i >= 0 && c[i] != 1; i--)
c.pop_back();
B.push_back(c);
}
void Print(unsigned long a, std::ostream &out) {
if(a < B.size()) {
for(int i = B[a].size() - 1; i >= 0; i--)
// std::cout << B[a][i];
out<<B[a][i];
}
else // std::cout << "ERROR: element does not exist!";
out<<"ERROR: element does not exist!";
}
};

int main(){
T Ttest;
Ttest.Conv(255);
Ttest.Print(0, std::cout);
}
// out: truetruetruetruetruetruetruetrue

So, 'std::bit_vector' works here with only the two vector changes (the
ostream ref was for my TestBench program (I use stringstreams to simulate
std::cout)(just use your Print() with no change)).

Maybe this will give you an idea:
// #includes: <string>, <iostream>, <sstream>, <bitset>, <vector>
int main(){
using std::cout; // for NG post
// std::bitset< 512 BitsBig( 255 ); // compiles for me

std::vector<std::bitset< sizeof(unsigned long) * CHAR_BIT Vbits;
std::bitset< sizeof(unsigned long) * CHAR_BIT Bits( 255 );
Vbits.push_back(Bits);
cout <<"Vbits.at(0) = "<<Vbits.at(0)<<std::endl;
cout <<"Vbits.at(0).to_ulong() = "
<<Vbits.at(0).to_ulong()<<std::endl;

std::ostringstream GetBits;
GetBits<<Vbits.at(0);
cout <<"GetBits.str() = "<<GetBits.str()<<std::endl;
std::string Sbits( GetBits.str() );
while( Sbits.at(0) == '0'){ Sbits.erase(0, 1);}
cout <<"Sbits = "<<Sbits<<std::endl;
cout<<std::endl;
return 0;
} // main()
/* - output - :
Vbits.at(0) = 00000000000000000000000011111111
Vbits.at(0).to_ulong() = 255
GetBits.str() = 00000000000000000000000011111111
Sbits = 11111111
*/

--
Bob R
POVrookie
May 15 '07 #6
On 15 Maj, 03:13, "BobR" <removeBadB...@worldnet.att.netwrote:
<zacar...@gmail.comwrote in message ...
On 14 Maj, 20:47, "BobR" <removeBadB...@worldnet.att.netwrote:
Thanks for the feedback, aspecial about the vector, as people keep
telling me that vector<boolonly take up one bit of memory per
element even though it is my experience that it takes up one byte of
memory.
Next i would propably be on its place to mention that any input number
(for now) wont be greater than 32/64 bit, but the out but can be
unlimited. The conversion in it self is not so important, i just dont
want to write a whole lot of code to do it, if there is a simple way.
the code as it looks now (havent played with the bit_vector yet):
@code
class T { [snip, see below ] };
@code
If we assume this is as it should be, my next task will be a little
more complicated.
x = input value
2^x is what i need to calculate and of course the result will be so
great that i wont be able to store it in a 32/64 integer and thats the
reason for the weird way of doing thing.

If you don't know x's size (or can't calculate it), how do you plan to pass
it to the method?
T::Calc(std::bit_vector const &Bvec){/* parse it */} // ??

Maybe you could explain a little more what you are trying to solve.
Explain exactly what you plan to input, and what you want for output.
The method im thinking about using is: "left-to-right binary
exponentiation" (http://primes.utm.edu/glossary/page.php?
sort=BinaryExponentiation).
i have some trouble explaining my self in english, but i hope im doing
ok.

You are doing fine. Even some educated 'native English speaking' people
sometimes have trouble explaining things. <G(not me. I ain't educated!).

I just did a quick test using bit_vector. I changed these:

class T {
// std::vector< std::vector<boolB;
std::vector< std::bit_vector >B; // <--- here
public:
void Clear(){ B.clear();}
void Conv(unsigned long a) {
// std::vector<bool>c;
std::bit_vector c; // <--- here (2)
for(int i = 0; i < 32; i++, a = a >1)
c.push_back(a & 1);
for(int i = 31; i >= 0 && c[i] != 1; i--)
c.pop_back();
B.push_back(c);
}
void Print(unsigned long a, std::ostream &out) {
if(a < B.size()) {
for(int i = B[a].size() - 1; i >= 0; i--)
// std::cout << B[a][i];
out<<B[a][i];
}
else // std::cout << "ERROR: element does not exist!";
out<<"ERROR: element does not exist!";
}
};

int main(){
T Ttest;
Ttest.Conv(255);
Ttest.Print(0, std::cout);
}
// out: truetruetruetruetruetruetruetrue

So, 'std::bit_vector' works here with only the two vector changes (the
ostream ref was for my TestBench program (I use stringstreams to simulate
std::cout)(just use your Print() with no change)).

Maybe this will give you an idea:
// #includes: <string>, <iostream>, <sstream>, <bitset>, <vector>
int main(){
using std::cout; // for NG post
// std::bitset< 512 BitsBig( 255 ); // compiles for me

std::vector<std::bitset< sizeof(unsigned long) * CHAR_BIT Vbits;
std::bitset< sizeof(unsigned long) * CHAR_BIT Bits( 255 );
Vbits.push_back(Bits);
cout <<"Vbits.at(0) = "<<Vbits.at(0)<<std::endl;
cout <<"Vbits.at(0).to_ulong() = "
<<Vbits.at(0).to_ulong()<<std::endl;

std::ostringstream GetBits;
GetBits<<Vbits.at(0);
cout <<"GetBits.str() = "<<GetBits.str()<<std::endl;
std::string Sbits( GetBits.str() );
while( Sbits.at(0) == '0'){ Sbits.erase(0, 1);}
cout <<"Sbits = "<<Sbits<<std::endl;
cout<<std::endl;
return 0;
} // main()
/* - output - :
Vbits.at(0) = 00000000000000000000000011111111
Vbits.at(0).to_ulong() = 255
GetBits.str() = 00000000000000000000000011111111
Sbits = 11111111
*/

--
Bob R
POVrookie- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -
i did some research on the bit_vector http://www.sgi.com/tech/stl/bit_vector.html
and it seems to me that it best not to use it?
anyway ive done the next sted of my code and made an adder:

@code
std::vector<boolAdd(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;
}
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[i] ^ B2[i]) ^ carry) | ((B1[i] & B2[i]) &
carry));
carry = (B1[i] & B2[i]) | (B1[i] & carry) | (B2[i] & carry);
}
if(carry == 1)
a.push_back(carry);
return a;
}
@code

May 15 '07 #7

<za******@gmail.comwrote in message ...

/* """
i did some research on the bit_vector
http://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<boolAdd(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<boolvecA(5, true);
std::vector<boolvecB;
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<boolvecC;
std::vector<boolvecD(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[i] ^ B2[i]) ^ carry) | ((B1[i] & B2[i]) &
carry));
carry = (B1[i] & B2[i]) | (B1[i] & carry) | (B2[i] & 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<boolAdd( 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<boolAdd( std::vector<boolB1,
std::vector<boolB2){
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. ]
--
Bob R
POVrookie
--
Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
May 15 '07 #8
On 15 Maj, 22:52, "BobR" <removeBadB...@worldnet.att.netwrote:
<zacar...@gmail.comwrote in message ...

/* """
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<boolAdd(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<boolvecA(5, true);
std::vector<boolvecB;
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<boolvecC;
std::vector<boolvecD(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[i] ^ B2[i]) ^ carry) | ((B1[i] & B2[i]) &
carry));
carry = (B1[i] & B2[i]) | (B1[i] & carry) | (B2[i] & 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<boolAdd( 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<boolAdd( std::vector<boolB1,
std::vector<boolB2){
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. ]
--
Bob R
POVrookie
--
Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
Wow, you are helpfull, thanks alot.

yes i can see it would be on its place to explain a thing or two about
what i am doing and why.
It is a hobby project.
First of all im just want to be better at coding by practice, im doing
that by giving my self a challence, in this case, writing a program to
test weather 2^p-1 is prime (mersenne prime). im gonna handle numbers
with millions of digits, so of course i cant be limited by 32/64 bit
vars.
There is a very simple method for testing for testing mersenne primes,
called the lucas lehmer test.

For example, to prove 2^7 - 1 is prime:

@copy
S (1) = 4
S (2) = (4 * 4 - 2) mod 127 = 14
S (3) = (14 * 14 - 2) mod 127 = 67
S (4) = (67 * 67 - 2) mod 127 = 42
S (5) = (42 * 42 - 2) mod 127 = 111
S (6) = (111 * 111 - 2) mod 127 = 0
-------------------------------------
2^P means 2 to the Pth power.
2^7 - 1 = 128 - 1 = 127
Each step except for the initialization step S(1) = 4
takes the value of the previous step, squares it,
subtracts 2, then mods it by the mersenne number.
@copy

However again we run in to the problem with the limitations if using
regular vars.
Imagine if the we were testing 2^207891-1

But enough with that, this is all really a way for me to figure out
methods to use, because my real goal is to write it in assembly,
including a suitable bootloader, and that is a real challence :D

Anyway, if i succeed, im bet i will be a great deal better both at c++
and assembly.

May 15 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
0
by: Lou Evart | last post by:
DOCUMENT CONVERSION SERVICES Softline International (SII) operates one of the industry's largest document and data conversion service bureaus. In the past year, SII converted over a million...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.