Re: Dec2Bin conversion
<zacariaz@gmail.com> wrote in message ...
On 14 Maj, 20:47, "BobR" <removeBadB...@worldnet.att.net> wrote:
Thanks for the feedback, aspecial about the vector, as people keep
telling me that vector<bool> only 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<bool> >B;
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