473,508 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::bitset, simple question

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
14 2996
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
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
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
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
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
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
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
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
"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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3603
by: Dill Hole | last post by:
Can anyone tell me why std::bitset<2> foo(std::string("01")) initializes the bitset in reverse order, i.e. foo=true foo=false I would expect the bitset to be initialized in string text...
5
5422
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
3
7670
by: Gaijinco | last post by:
In C++ this code: for(int i=0; i<16; ++i){ std::cout << std::bitset<4>(i) << std::endl; Will print numbers 1-15 as binaries with 4 bits. Is there any equivalent of bitset in C?
7
8180
by: felixnielsen | last post by:
I would wery much like this to work: @code start #include <iostream> #include <bitset> const unsigned short size = 2; // 2^<unsigned int> // union V { char c; std::bitset<size*size*size> b;...
5
5575
by: Sean Farrow | last post by:
Hi: I have an iterator defined as follows: std::map<std::bitset<6>, int>::iterator DotsIterator, SignsIterator; I get errors that std::bitset does not declare the < operator. Does this mean I...
3
4704
by: Guy.Tristram | last post by:
Is there any good reason operator< is not defined for std::bitset? It seems to me that: 1 - it would be useful. 2 - it is easy to implement inside the class template. 3 - it is impossible to...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.