C++ How can I assign an input value to a std::bitset argument?

后端 未结 2 1434
暖寄归人
暖寄归人 2021-01-19 15:19

I want to make a simple program that will take number of bits from the input and as an output show binary numbers, written on given bits (example: I type 3: it shows 000

2条回答
  •  Happy的楠姐
    2021-01-19 15:53

    You have to define const int bits=10; as a global constant :

    #include 
    #include 
    #include 
    
    using namespace std;
    const unsigned bits=10;
    int main() {
        int maximum_value = 0,x_temp=10;
    
        for (int i = 1; i <= bits; i++) {
            maximum_value += pow(2, bits - i);
        }
        for (int i = maximum_value; i >= 0; i--)
            cout << bitset(maximum_value - i) << endl;
        return 0;
    }
    

提交回复
热议问题