std::bitset, simple question

Discussion in 'C++' started by Haro Panosyan, Dec 6, 2005.

  1. How to construct a bitset from string?

    For example:
    std::bitset<16> b16("1011011110001011");

    Is this possible?

    Thanks in advance.
    -haro
     
    Haro Panosyan, Dec 6, 2005
    #1
    1. Advertising

  2. Haro Panosyan wrote:
    > How to construct a bitset from string?
    >
    > For example:
    > std::bitset<16> b16("1011011110001011");
    >
    > Is this possible?


    Yes. Have you tried RTFMing?
     
    Victor Bazarov, Dec 6, 2005
    #2
    1. Advertising

  3. Victor Bazarov wrote:
    > Haro Panosyan wrote:
    >
    >> How to construct a bitset from string?
    >>
    >> For example:
    >> std::bitset<16> b16("1011011110001011");
    >>
    >> Is this possible?

    >
    >
    > Yes. Have you tried RTFMing?


    If RTFMing is about searching the web,
    then yes, I did(because I knew you were going to ask:) )
     
    Haro Panosyan, Dec 6, 2005
    #3
  4. Haro Panosyan wrote:
    > Victor Bazarov wrote:
    >
    >> Haro Panosyan wrote:
    >>
    >>> How to construct a bitset from string?
    >>>
    >>> For example:
    >>> std::bitset<16> b16("1011011110001011");
    >>>
    >>> Is this possible?

    >>
    >>
    >>
    >> Yes. Have you tried RTFMing?

    >
    >
    > If RTFMing is about searching the web,
    > then yes, I did(because I knew you were going to ask:) )


    REALLY?

    I just went to www.google.com, typed in "bitset from string" and
    the first link I got was 'http://www.sgi.com/tech/stl/bitset.html'
    which describes the interface in detail. What web did you search?
     
    Victor Bazarov, Dec 6, 2005
    #4
  5. Victor Bazarov wrote:
    > Haro Panosyan wrote:
    >
    >> How to construct a bitset from string?
    >>
    >> For example:
    >> std::bitset<16> b16("1011011110001011");
    >>
    >> Is this possible?

    >
    >
    > Yes. Have you tried RTFMing?


    I am using g++ 3.3.3 for SunOS. Here is the code:

    #include <bitset>

    int main()
    {
    std::bitset<8> b2("10101010");// <- line 5
    return 0;
    }


    And here is the error message:

    >g++ bitset1.cpp -o bitset1

    bitset1.cpp: In function `int main()':
    bitset1.cpp:5: error: invalid conversion from `const char*' to `long
    unsigned
    int'
    bitset1.cpp:5: error: initializing argument 1 of `
    std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb = 8]'
     
    Haro Panosyan, Dec 6, 2005
    #5
  6. Haro Panosyan

    Ben Pope Guest

    Haro Panosyan wrote:
    > How to construct a bitset from string?
    >
    > For example:
    > std::bitset<16> b16("1011011110001011");
    >
    > Is this possible?


    I don't think so.

    I was thinking stringstreams, but there is no base 2 manipulator like
    oct, hex, dec.

    Then I thought std::transform, but bitset has no iterators!

    So, presumably you have to do it the hard way:



    #include <string>
    #include <iostream>
    #include <bitset>
    #include <algorithm>

    int main(int argc, char* argv[])
    {
    std::string str("1011011110001011");

    std::bitset<16> bs;

    size_t length = std::min(str.length(), bs.size());

    for (size_t i = 0; i < length; ++i) {
    bs = str[length - i - 1] == '1';
    }

    std::cout << bs.to_ulong() << std::endl;
    }


    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Dec 6, 2005
    #6
  7. Haro Panosyan

    Ben Pope Guest

    Haro Panosyan wrote:
    > How to construct a bitset from string?
    >
    > For example:
    > std::bitset<16> b16("1011011110001011");
    >
    > Is this possible?


    Ignore my other post (which refuses to show up for now).

    You can certainly construct it from a std::string, but not a const char[17]

    I'll leave that up to you :p

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Dec 6, 2005
    #7
  8. Victor Bazarov wrote:
    > Haro Panosyan wrote:
    >
    >> Victor Bazarov wrote:
    >>
    >>> Haro Panosyan wrote:
    >>>
    >>>> How to construct a bitset from string?
    >>>>
    >>>> For example:
    >>>> std::bitset<16> b16("1011011110001011");
    >>>>
    >>>> Is this possible?
    >>>
    >>>
    >>>
    >>>
    >>> Yes. Have you tried RTFMing?

    >>
    >>
    >>
    >> If RTFMing is about searching the web,
    >> then yes, I did(because I knew you were going to ask:) )

    >
    >
    > REALLY?
    >
    > I just went to www.google.com, typed in "bitset from string" and
    > the first link I got was 'http://www.sgi.com/tech/stl/bitset.html'
    > which describes the interface in detail. What web did you search?


    Same web, check my other post with compiler error. Linux is same.
    I may be missing something, but cannot find what, hence I am here for
    help.

    Beleive me, not everybody comes here without RTFMing!
    ( Finally I know what is it)
     
    Haro Panosyan, Dec 6, 2005
    #8
  9. Haro Panosyan wrote:
    > Victor Bazarov wrote:
    >
    >> Haro Panosyan wrote:
    >>
    >>> How to construct a bitset from string?
    >>>
    >>> For example:
    >>> std::bitset<16> b16("1011011110001011");
    >>>
    >>> Is this possible?

    >>
    >>
    >>
    >> Yes. Have you tried RTFMing?

    >
    >
    > I am using g++ 3.3.3 for SunOS. Here is the code:
    >
    > #include <bitset>
    >
    > int main()
    > {
    > std::bitset<8> b2("10101010");// <- line 5
    > return 0;
    > }
    >
    >
    > And here is the error message:
    >
    > >g++ bitset1.cpp -o bitset1

    > bitset1.cpp: In function `int main()':
    > bitset1.cpp:5: error: invalid conversion from `const char*' to `long
    > unsigned
    > int'
    > bitset1.cpp:5: error: initializing argument 1 of `
    > std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb = 8]'


    Well "10101010" is not really a string, is it? It's a _string_literal_.
    The FM says that you should be able to initialise a bitset from
    'std::string' (if you did Read The FM, of course). So, a relatively easy
    progression from your code to working one should be

    std::bitset<8> b2(std::string("10101010"));

    [Perhaps I presume too much about people who end up asking here after
    trying to solve their problems. My fault. Please forgive me.]
     
    Victor Bazarov, Dec 6, 2005
    #9
  10. Haro Panosyan schrieb:
    > Victor Bazarov wrote:
    >
    >> Haro Panosyan wrote:
    >>
    >>> How to construct a bitset from string?
    >>>
    >>> For example:
    >>> std::bitset<16> b16("1011011110001011");
    >>>
    >>> Is this possible?

    >>
    >> Yes. Have you tried RTFMing?

    >
    > If RTFMing is about searching the web,
    > then yes, I did(because I knew you were going to ask:) )


    RTFM is more about reading the manual:
    http://www.google.com/search?q=define:rtfm

    But searching the web would do it:
    http://www.google.com/search?q=std bitset

    There you could see that std::bitset<> has a constructor from std::string.

    Thomas
     
    Thomas J. Gritzan, Dec 6, 2005
    #10
  11. Haro Panosyan

    P.J. Plauger Guest

    "Haro Panosyan" <> wrote in message
    news:dn54cu$ft6$...

    > Victor Bazarov wrote:
    >> Haro Panosyan wrote:
    >>
    >>> How to construct a bitset from string?
    >>>
    >>> For example:
    >>> std::bitset<16> b16("1011011110001011");
    >>>
    >>> Is this possible?

    >>
    >>
    >> Yes. Have you tried RTFMing?

    >
    > I am using g++ 3.3.3 for SunOS. Here is the code:
    >
    > #include <bitset>
    >
    > int main()
    > {
    > std::bitset<8> b2("10101010");// <- line 5
    > return 0;
    > }
    >
    >
    > And here is the error message:
    >
    > >g++ bitset1.cpp -o bitset1

    > bitset1.cpp: In function `int main()':
    > bitset1.cpp:5: error: invalid conversion from `const char*' to `long
    > unsigned
    > int'
    > bitset1.cpp:5: error: initializing argument 1 of `
    > std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb =
    > 8]'


    std::bitset<8> b2(std::string("10101010"));

    works for me.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Dec 6, 2005
    #11
  12. Haro Panosyan

    Guest

    Haro Panosyan wrote:
    > How to construct a bitset from string?
    >
    > For example:
    > std::bitset<16> b16("1011011110001011");
    >
    > Is this possible?
    >
    > Thanks in advance.
    > -haro


    #include <bitset>
    #include <string>

    int main()
    {
    std::bitset<16> b16( std::string("1011011110001011") );
    }

    Cheers,
    Andre
     
    , Dec 6, 2005
    #12
  13. Thanks Thomas,

    This now works:

    #include <bitset>
    #include <string>

    int main()
    {
    std::bitset<8> b2(std::string("10101010"));
    return 0;
    }

    -haro


    Thomas J. Gritzan wrote:
    > Haro Panosyan schrieb:
    >
    >>Victor Bazarov wrote:
    >>
    >>
    >>>Haro Panosyan wrote:
    >>>
    >>>
    >>>>How to construct a bitset from string?
    >>>>
    >>>>For example:
    >>>>std::bitset<16> b16("1011011110001011");
    >>>>
    >>>>Is this possible?
    >>>
    >>>Yes. Have you tried RTFMing?

    >>
    >>If RTFMing is about searching the web,
    >>then yes, I did(because I knew you were going to ask:) )

    >
    >
    > RTFM is more about reading the manual:
    > http://www.google.com/search?q=define:rtfm
    >
    > But searching the web would do it:
    > http://www.google.com/search?q=std bitset
    >
    > There you could see that std::bitset<> has a constructor from std::string.
    >
    > Thomas
     
    Haro Panosyan, Dec 6, 2005
    #13
  14. Thanks Ben,

    As pointed by Thomas in this thread, I was using
    undefined constructor.

    -haro


    Ben Pope wrote:
    > Haro Panosyan wrote:
    >
    >> How to construct a bitset from string?
    >>
    >> For example:
    >> std::bitset<16> b16("1011011110001011");
    >>
    >> Is this possible?

    >
    >
    > I don't think so.
    >
    > I was thinking stringstreams, but there is no base 2 manipulator like
    > oct, hex, dec.
    >
    > Then I thought std::transform, but bitset has no iterators!
    >
    > So, presumably you have to do it the hard way:
    >
    >
    >
    > #include <string>
    > #include <iostream>
    > #include <bitset>
    > #include <algorithm>
    >
    > int main(int argc, char* argv[])
    > {
    > std::string str("1011011110001011");
    >
    > std::bitset<16> bs;
    >
    > size_t length = std::min(str.length(), bs.size());
    >
    > for (size_t i = 0; i < length; ++i) {
    > bs = str[length - i - 1] == '1';
    > }
    >
    > std::cout << bs.to_ulong() << std::endl;
    > }
    >
    >
    > Ben Pope
     
    Haro Panosyan, Dec 6, 2005
    #14
  15. Victor Bazarov wrote:
    > Haro Panosyan wrote:
    >
    >> Victor Bazarov wrote:
    >>
    >>> Haro Panosyan wrote:
    >>>
    >>>> How to construct a bitset from string?
    >>>>
    >>>> For example:
    >>>> std::bitset<16> b16("1011011110001011");
    >>>>
    >>>> Is this possible?
    >>>
    >>>
    >>>
    >>>
    >>> Yes. Have you tried RTFMing?

    >>
    >>
    >>
    >> I am using g++ 3.3.3 for SunOS. Here is the code:
    >>
    >> #include <bitset>
    >>
    >> int main()
    >> {
    >> std::bitset<8> b2("10101010");// <- line 5
    >> return 0;
    >> }
    >>
    >>
    >> And here is the error message:
    >>
    >> >g++ bitset1.cpp -o bitset1

    >> bitset1.cpp: In function `int main()':
    >> bitset1.cpp:5: error: invalid conversion from `const char*' to `long
    >> unsigned
    >> int'
    >> bitset1.cpp:5: error: initializing argument 1 of `
    >> std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb
    >> = 8]'

    >
    >
    > Well "10101010" is not really a string, is it? It's a _string_literal_.
    > The FM says that you should be able to initialise a bitset from
    > 'std::string' (if you did Read The FM, of course). So, a relatively easy
    > progression from your code to working one should be
    >
    > std::bitset<8> b2(std::string("10101010"));
    >
    > [Perhaps I presume too much about people who end up asking here after
    > trying to solve their problems. My fault. Please forgive me.]


    No problem, and I am with you.
    Thanks again.
     
    Haro Panosyan, Dec 6, 2005
    #15
    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. Dill Hole
    Replies:
    2
    Views:
    438
    Mike Wahler
    Jul 5, 2003
  2. Hunter Hou

    one bitset simple question

    Hunter Hou, Jun 24, 2004, in forum: C++
    Replies:
    6
    Views:
    1,993
    SaltPeter
    Jun 25, 2004
  3. SpOiLeR
    Replies:
    5
    Views:
    1,350
    SpOiLeR
    Mar 16, 2005
  4. Gaijinco

    C quivalent of std::bitset

    Gaijinco, Sep 29, 2005, in forum: C Programming
    Replies:
    3
    Views:
    353
    Gaijinco
    Sep 29, 2005
  5. Ninds
    Replies:
    14
    Views:
    750
    W Karas
    Dec 3, 2012
Loading...

Share This Page