A
arnuld
i have solved the problem. any comments on it:
/* C++ Primer 4/e
* chapter 3
*
* exercise 3.24
* STATEMENT:
* consider the sequence 1,2,3,5,13,21. Initialize a "bitset<32>"
object that has a one bit in each position corresponding to a number in
this sequence. */
#include <iostream>
#include <string>
#include <bitset>
int main()
{
std::string bstr("100000001000010010111"); std::bitset<32>
str_bit(bstr);
/* i could have created an empty bitset first and then assign
the values using subscripting but the problem statement requires that
initialisation must set the bits at positions 1,2,3,4,13,21 as ON. so i
used the string as initialisation */
std::cout << "printing bits" << std::endl; for(size_t ix=0; ix !=
str_bit.size(); ++ix)
{
std::cout << "position "
<< ix + 1
<< " : "
<< str_bit[ix]
<< std::endl;
}
return 0;
}
========= OUTPUT =============
{arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra ex_03-24.cpp
{arnuld@arch cpp }% ./a.out
printing bits
position 1 : 1
position 2 : 1
position 3 : 1
position 4 : 0
position 5 : 1
position 6 : 0
position 7 : 0
position 8 : 1
position 9 : 0
position 10 : 0
position 11 : 0
position 12 : 0
position 13 : 1
position 14 : 0
position 15 : 0
position 16 : 0
position 17 : 0
position 18 : 0
position 19 : 0
position 20 : 0
position 21 : 1
position 22 : 0
position 23 : 0
position 24 : 0
position 25 : 0
position 26 : 0
position 27 : 0
position 28 : 0
position 29 : 0
position 30 : 0
position 31 : 0
position 32 : 0
{arnuld@arch cpp }%
/* C++ Primer 4/e
* chapter 3
*
* exercise 3.24
* STATEMENT:
* consider the sequence 1,2,3,5,13,21. Initialize a "bitset<32>"
object that has a one bit in each position corresponding to a number in
this sequence. */
#include <iostream>
#include <string>
#include <bitset>
int main()
{
std::string bstr("100000001000010010111"); std::bitset<32>
str_bit(bstr);
/* i could have created an empty bitset first and then assign
the values using subscripting but the problem statement requires that
initialisation must set the bits at positions 1,2,3,4,13,21 as ON. so i
used the string as initialisation */
std::cout << "printing bits" << std::endl; for(size_t ix=0; ix !=
str_bit.size(); ++ix)
{
std::cout << "position "
<< ix + 1
<< " : "
<< str_bit[ix]
<< std::endl;
}
return 0;
}
========= OUTPUT =============
{arnuld@arch cpp }% g++ -ansi -pedantic -Wall -Wextra ex_03-24.cpp
{arnuld@arch cpp }% ./a.out
printing bits
position 1 : 1
position 2 : 1
position 3 : 1
position 4 : 0
position 5 : 1
position 6 : 0
position 7 : 0
position 8 : 1
position 9 : 0
position 10 : 0
position 11 : 0
position 12 : 0
position 13 : 1
position 14 : 0
position 15 : 0
position 16 : 0
position 17 : 0
position 18 : 0
position 19 : 0
position 20 : 0
position 21 : 1
position 22 : 0
position 23 : 0
position 24 : 0
position 25 : 0
position 26 : 0
position 27 : 0
position 28 : 0
position 29 : 0
position 30 : 0
position 31 : 0
position 32 : 0
{arnuld@arch cpp }%