Binary

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
would you consider std::bitset or the dynamic bitset of boost?
Last edited on
closed account (z05DSL3A)
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
std::cout << str[MAX_UNSIGNED_INT_VAL] is probably bad news though, when i underflows.
@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
closed account (z05DSL3A)
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
That code seems a lot faster (and more unreadable, at that :P) then mine code, thanks.

I'm making a little widget that displays some attributes of (unsigned integer) numbers, much like the one used in: http://www.wolframalpha.com/
closed account (z05DSL3A)
Final tweak, hopefully a bit more understandable/readable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void ToBinary(unsigned u)
{
    int bit_count = sizeof(u) * 8;
    unsigned bit = 0x1 << (bit_count - 1);
    bool output(false);

    for(int i(0); i < bit_count; ++i, /*walk the bit*/ bit >>= 1)
    {
        if(u & bit) 
        {   
            output = true;
            std::cout << "1";
        }
        else
        {
            if(output) std::cout << "0";
        }
    }
}
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
Topic archived. No new replies allowed.