Re: Dec2Bin conversion
On 14 Maj, 20:47, "BobR" <removeBadB...@worldnet.att.net> wrote:
<zacar...@gmail.com> wrote 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<bool> was 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<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 {
std::vector< std::vector<bool> >B;
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.