C++ Primer ex 3.24 (bitset class)

Discussion in 'C++' started by arnuld, Jul 20, 2007.

  1. arnuld

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


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



    --
    -- http://arnuld.blogspot.com
     
    arnuld, Jul 20, 2007
    #1
    1. Advertising

  2. On 2007-07-20 09:23, arnuld wrote:
    > 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.

    --
    Erik Wikström
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Jul 20, 2007
    #2
    1. Advertising

  3. arnuld

    Ian Collins Guest

    Erik Wikström wrote:
    > On 2007-07-20 09:23, arnuld wrote:
    >>
    >> #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.

    --
    Ian Collins.
     
    Ian Collins, Jul 20, 2007
    #3
  4. arnuld

    arnuld Guest

    > 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.

    --
    -- http://arnuld.blogspot.com
     
    arnuld, Jul 20, 2007
    #4
  5. arnuld

    Jim Langston Guest

    "arnuld" <> wrote in message
    news:p...
    >> 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.


    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.

    >> shifts them to the right.
     
    Jim Langston, Jul 20, 2007
    #5
  6. arnuld

    Bo Persson Guest

    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
     
    Bo Persson, Jul 20, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erick Crouse

    BitSet Class Implementation

    Erick Crouse, Nov 16, 2006, in forum: Java
    Replies:
    4
    Views:
    506
    Daniel Pitts
    Nov 20, 2006
  2. Sarath
    Replies:
    4
    Views:
    464
  3. arnuld
    Replies:
    2
    Views:
    471
    Ian Collins
    Jul 20, 2007
  4. Replies:
    5
    Views:
    413
    Salt_Peter
    Jan 5, 2008
  5. Ninds
    Replies:
    14
    Views:
    825
    W Karas
    Dec 3, 2012
Loading...

Share This Page