C++ Primer ex 3.24 (bitset class)

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 }%
 
G

Guest

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;
}

A matter of taste perhaps but I find it slightly more readable to use
bitwise operations and shifting to initialise the bitset:

#include <iostream>
#include <bitset>

int main()
{
// Set bits nr. 1, 2, 3, 5, 13, and 21
std::bitset<32> str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 | 1<<21);
std::cout str_bit;
return 0;
}

The advantage being that it's explicit what bit I'm setting, I don't
have to count the number of characters in a string, which might be a
blessing if I ever need to change anything.
 
I

Ian Collins

Erik said:
#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;
}

A matter of taste perhaps but I find it slightly more readable to use
bitwise operations and shifting to initialise the bitset:

#include <iostream>
#include <bitset>

int main()
{
// Set bits nr. 1, 2, 3, 5, 13, and 21
std::bitset<32> str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 | 1<<21);
std::cout str_bit;
return 0;
}

The advantage being that it's explicit what bit I'm setting, I don't
have to count the number of characters in a string, which might be a
blessing if I ever need to change anything.
You also set the correct bits, the original was off by one.
 
A

arnuld

A matter of taste perhaps but I find it slightly more readable to use
bitwise operations and shifting to initialise the bitset:

#include <iostream>
#include <bitset>

int main()
{
// Set bits nr. 1, 2, 3, 5, 13, and 21
std::bitset<32> str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 | 1<<21);
std::cout str_bit;
return 0;
}

The advantage being that it's explicit what bit I'm setting, I don't
have to count the number of characters in a string, which might be a
blessing if I ever need to change anything.

yep, that is really good looking, short and expressive :) but C++ Primer
didnot tell me anything about "n << m" yet. i think it is called
operator-overloading.
 
J

Jim Langston

arnuld said:
yep, that is really good looking, short and expressive :) but C++ Primer
didnot tell me anything about "n << m" yet. i think it is called
operator-overloading.

Actually, every other use of << other than bit shifting is operator
overloading. The << and >> keywords are bitwise shifting. They come from C
and predate C++'s overloaded use of them.

1 << 1; means, shift the bit value 1 left 1 bit. 001 becomes 010
1 << 2; means, shift the bit value 1 left 2 bits. 001 becomes 100
etc.
 
B

Bo Persson

arnuld wrote:
::: On Fri, 20 Jul 2007 08:45:41 +0000, Erik Wikström wrote:
::: A matter of taste perhaps but I find it slightly more readable to
::: use bitwise operations and shifting to initialise the bitset:
:::
::: #include <iostream>
::: #include <bitset>
:::
::: int main()
::: {
::: // Set bits nr. 1, 2, 3, 5, 13, and 21
::: std::bitset<32> str_bit(1<<1 | 1<<2 | 1<<3 | 1<<5 | 1<<13 |
::: 1<<21); std::cout str_bit;
::: return 0;
::: }
:::
::: The advantage being that it's explicit what bit I'm setting, I
::: don't have to count the number of characters in a string, which
::: might be a blessing if I ever need to change anything.
::
:: yep, that is really good looking, short and expressive :) but C++
:: Primer didnot tell me anything about "n << m" yet. i think it is
:: called operator-overloading.

It is shorter and correct, but also limited to bits that fit in a
value of type long. The string you used could theoretically be much
longer. (Not that it would make it any easier to count the positions
right :).


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

some questions about "bitset" class 2
C++ Primer ex 3.14 8
C++ Primer ex 7.5 18
C++ Primer ex 5.18 5
Lexical Analysis on C++ 1
C++ Primer ex 4.16 2
C++ Primer ex 7.6 17
C++ Primer ex 9.27 4

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top