bitset and numerical sequence C++ -


im learning c++ @ minute book stanley lippman. im studying paragraph named "class bitset". there exercise gives me numerical sequence transfrom bitset<32>.

the numerical sequence is: 1,2,3,5,8,13,21. can ask code right peresenting numerical sequence? lippman want me use each bit in bitset represent sequence, bitset can store more 1 value in it? im doing first time , idea got is:

int index = 0; const int size_ = 7; unsigned long f2[size_];  int main() {    setlocale(lc_all,"rus");     string try1;     cout << "type numerical sequence in binary code: " << endl;        while (cin >> try1) {           bitset<32> go(try1);           if ( go.to_ulong() > 21 )  { cout << "end of sequence" << endl; break; }                   f2[index] = go.to_ulong();           index++;           try1.clear();           go.reset();        }     ( index; index >= 0; index-- ) {        cout << f2[index] << " ";    }     system("pause");     return 0; } 

is bitset can store more 1 value in it?

well, yes — it's set of bits, hence name. each bit can on or off, may deemed 1 "value". thus, std::bitset<32> can encode 32 "values" inside it. ultimately, whole bitset has 1 value @ time, that's beauty of "encoding" data — every value composed of smaller building blocks. solve problem, need remember every value stored on computer consists of series of bits.

presumably supposed encode sequence x,y,...,z setting bits @ positions x,y,...,z. may use function set.

for example, once you've set bits 1,2,3,5,8,13,21 (and i'm assuming 0-based system here can support input 0), bitset "contain":

#0   #4   #8   #12  #16  #20  #24  #28  #32 +----+----+----+----+----+----+----+----+ |0111 0100 1000 0100 0000 0100 0000 0000| +-^^^+-^--+^---+-^--+----+-^--+----+----+   |||  |   |     |         | 

when translated binary decimal, sequence number ‭1,954,808,832‬, you'll if call to_ulong() on bitset object.

in code, you're instead creating fresh bitset each value entered, useless; actual storage array of unsigned long, not in keeping spirit of exercise (which use bitset storage instead). you're calling reset() before object goes out of scope (at end of loop body), nothing.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -