I realize that there is another thread about this topic, where bitshifts are used, but I do not understand why my code is not working:

#include <iostream>
#include <bitset>
using namespace std;
    int main()
    { 
        bitset<5> b;
        int temp=0;
        for(int count=0; count<=10; count++)
        {   temp=count;
            for(int check=4; check>=0; check--)
            {
                if(2^check<temp)
                {
                    b[check]=1;
                    temp-=2^check;
                }
                else b[check]=0;
            }
            cout<<count<<" "<<b<<endl;
            b.reset();
        }

    }

could someone please help?

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I don't see where you're doing any conversion. You say you're converting to decimal. Is there an integer variable that holds the decimal? you have temp, but I assume that's just a temporary variable. What holds the final decimal value? I also don't see any initialization of the bitset that you are converting from. It's just this:

bitset<5> b;

And I assume that you realize that the ^ operator doesn't raise to a power, but is instead the XOR operator?

Please use code tags:

[code]
place code here
[/code]

Edited 3 Years Ago by happygeek: fixed formatting

hahaha. how could i forget about the xor. that was the problem. Thanks man

Also the initialization wasn't the problem since the default constructor fills all bits with 0's. I was just wondering, do you think there is a more efficient way to go about solving this problem?

#include <iostream>
#include <bitset>
using namespace std;
int power(int x, int y);
int main()
    { 
        bitset<5> b;
        int temp=0;
        for(int count=0; count<=10; count++)
        {   temp=count;
            for(int check=4; check>=0; check--)
            {
                if(power(2,check)<=temp)
                {
                    b[check]=1;
                    temp-=power(2,check);
                }
                else b[check]=0;
            }
            cout<<count<<" "<<b<<endl;
            b.reset();
        }

    }
    int power(int x, int y)
    {   if(y==0)
           return 1;
        x*=power(x,y-1);
        return x;
    }

If it's all zeroes, there's nothing to convert. The answer's 0. I figured you'd have a bitset that might be anything to start out with and you wanted to convert and store the result in an integer and then display the integer, but again, I see no integer to hold the decimal.

I figured the binary was stored in the bitset and you were converting, but there is no if-statement that ever checks the bitset and no calculation is done based on the bitset, nor is any for-loop based on the bitset, so why bother having a bitset? I think I am missing the whole purpose of the program.

There's no point in having a bitset if you're doing the whole conversion manually, if you've a bitset, and you want to convert the value in the bitset to an unsigned long, then you could just use the to_ulong() method.
You can find more info about this method here:
http://www.cplusplus.com/reference/stl/bitset/to_ulong/

(it would not have been part of STL if there wasn't an easy way to do it :P)

This question has already been answered. Start a new discussion instead.