Bitset in C++, about continuously add

前端 未结 2 454
鱼传尺愫
鱼传尺愫 2021-01-24 16:02

I want to add 1 value to the bitset until it over flows, I am wondering how to do this, like:

bitset<20> foo; (foo=00000 00000 00000 00000) how can we implement thi

2条回答
  •  逝去的感伤
    2021-01-24 16:30

    The following code should help you:

    bitset<20> b;
    
    bool overflow = false;
    while( ! overflow ){
        cout << b << endl; // prints bits in reversed order
        int ipos = b.size();
        overflow = true;
        while( overflow && --ipos >= 0 ){ 
            overflow = b[ipos];
            b[ipos] = ! b[ipos];
        }
    }
    

    tested by @Jarod42, thanks.

提交回复
热议问题