I am asked on C++ primer 5th edition this exercise:
Exercise 17.10: Using the sequence 1, 2, 3, 5, 8, 13, 21, initialize a bitset
that has a 1
bit in each position corresponding to a number in this
sequence. Default initialize another bitset
and write a small program to turn on each of the appropriate bits.
In fact I almost solved all the exercises of the book but I couldn't understand this. I've understood std::bitset
.
I've found a solution like this:
// init from the sequence: 1, 2, 3, 5, 8, 13, 21
std::bitset<22> bitseq("1000000010000100101110");
std::cout << bitseq << std::endl;
// Default initialize, then turn on.
std::bitset<22> bit_default;
for (auto i : {1, 2, 3, 5, 8, 13, 21})
bit_default.set(i);
std::cout << bit_default << std::endl;
assert(bitseq == bit_default);
But I don't know how it is this way and how it works? please help me to understand this if it is correct. Thanks guys too much!
I don't understand how can such 1000000010000100101110
represent the sequence 1, 2, 3, 5, 8, 13, 21
?.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…