By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

std::bitset, simple question

P: n/a
How to construct a bitset from string?

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

Is this possible?

Thanks in advance.
-haro
Dec 6 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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?
Dec 6 '05 #2

P: n/a
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:) )


Dec 6 '05 #3

P: n/a
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]'
Dec 6 '05 #4

P: n/a
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?
Dec 6 '05 #5

P: n/a
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)
Dec 6 '05 #6

P: n/a
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%3Artfm

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
Dec 6 '05 #7

P: n/a
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[i] = 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...
Dec 6 '05 #8

P: n/a
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.]
Dec 6 '05 #9

P: n/a
"Haro Panosyan" <ha**@ti.com> wrote in message
news:dn**********@home.itg.ti.com...
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
Dec 6 '05 #10

P: n/a
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...
Dec 6 '05 #11

P: n/a

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 '05 #12

P: n/a
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%3Artfm

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

Dec 6 '05 #13

P: n/a
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.
Dec 6 '05 #14

P: n/a
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[i] = str[length - i - 1] == '1';
}

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

Dec 6 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.