Jan 14, 2011 at 1:30pm UTC
I'm being really stupid, can't get the code to print the binary equivalent. This is what I have ( I get a segfault ):
1 2 3 4 5 6 7 8 9 10 11 12
void ToBinary(unsigned u)
{
std::vector<bool > str;
unsigned i(1);
while (u!=0)
{
str.push_back(u);
if (u&i)u-=i;
i*=2;
}
for (i=str.size();i>=0;i--) std::cout << str[i];
}
EDIT: Made it a bit more readable.
Last edited on Jan 14, 2011 at 1:44pm UTC
Jan 14, 2011 at 2:20pm UTC
would you consider std::bitset or the dynamic bitset of boost?
Last edited on Jan 14, 2011 at 2:25pm UTC
Jan 14, 2011 at 2:27pm UTC
The for loop would be an infinite loop by the looks of it.
i is an
unsigned int so will never go below zero. I dout if thats the problem with segfault though.
1 2 3 4 5 6 7 8
void ToBinary2(unsigned u)
{
int bits = sizeof (u) * 8;
for (int i(0); i < bits; ++i)
{
std::cout << ((u & (0x1 << (bits - 1 - i))) ? 1 : 0);
}
}
Last edited on Jan 14, 2011 at 2:53pm UTC
Jan 14, 2011 at 2:56pm UTC
std::cout << str[MAX_UNSIGNED_INT_VAL]
is probably bad news though, when i underflows.
Jan 14, 2011 at 2:58pm UTC
@stereoMatching
The problem with that is that the length must be a compile-time constant, while I want it to be dynamic. (And for some personal reasons, I am not installing boost.)
@Grey Wolf
That's some nice code, but I don't want it to output a countless amount of 0's before the actual number.. :/
Last edited on Jan 14, 2011 at 3:04pm UTC
Jan 14, 2011 at 3:19pm UTC
A bit more long winded, but eats the leading '0's
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
void ToBinary3(unsigned u)
{
int bits = sizeof (u) * 8;
std::vector<bool > str;
bool log(false );
for (int i(0); i < bits; ++i)
{
if (u & (0x1 << (bits - 1 - i)))
{
log = true ;
str.push_back(true );
}
else
{
if (log) str.push_back(false );
}
}
std::vector<bool >::iterator it;
for (it = str.begin(); it != str.end(); it++)
std::cout << *it;
std::cout << std::endl;
}
you can forget about the std::vector<bool> alwell if you like, I was just playing with your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void ToBinary2(unsigned u)
{
int bits = sizeof (u) * 8;
bool output(false );
for (int i(0); i < bits; ++i)
{
if (u & (0x1 << (bits - 1 - i)))
{
output= true ;
std::cout << "1" ;
}
else
{
if (output) std::cout << "0" ;
}
}
}
Last edited on Jan 14, 2011 at 3:27pm UTC
Jan 14, 2011 at 4:39pm UTC
Weird. I've got better performance with:
1 2 3 4 5 6 7 8 9 10 11
void ToBinary(unsigned u){
void ToBinary2(unsigned );
if (u) ToBinary2(u);
else std::cout<<'0' ;
}
void ToBinary2(unsigned u){
if (u==1){ std::cout<<'1' ; return ;}
ToBinary2(u>>1);
std::cout<< ((u&1)?'1' :'0' );
}
btw: If u==0 your code doesn't print the 0
Edit:pasted full code.
I'm bad at creating benchmarks. However there was not significant difference between your second code (
http://www.cplusplus.com/forum/beginner/34403/#msg185964 faster) and mine. (yours ToBinary2 was way behind)
1 2 3 4 5 6 7 8 9 10 11
//benchmark
extern void ToBinary(unsigned );
int main(){
int n=1000000;
for (unsigned L=0; L<10; L++)
for (unsigned K=1; K<n; K++){
ToBinary(K);
std::cout << std::endl;
}
return 0;
}
Last edited on Jan 14, 2011 at 5:13pm UTC