473,657 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

one bitset simple question

Hello,

I have this very simple program, but it can't be compiled. What's wrong
here?

thanks,
hunter
#include <iostream>
#include <bitset>

int main()
{
std::bitset<6> b1("101010");

std::cout << b1 << std::endl;

return 0;

}
The error is:
bitset.cpp: In function `int main()':
bitset.cpp:8: invalid conversion from `const char*' to `long unsigned int'
bitset.cpp:8: initializing argument 1 of `std::bitset<_N b>::bitset(lon g
unsigned int) [with unsigned int _Nb = 6]'
Compiler says can't convert, but bitset<> has such an explicit constructor.

P.S. > If I use default constructor, everything is fine.
Jul 22 '05 #1
6 2320
"Hunter Hou" <hy***@lucent.c om> wrote in message
news:cb******** @netnews.proxy. lucent.com...
std::bitset<6> b1("101010");

std::cout << b1 << std::endl;

return 0;

}
The error is:
bitset.cpp: In function `int main()':
bitset.cpp:8: invalid conversion from `const char*' to `long unsigned int'
bitset.cpp:8: initializing argument 1 of `std::bitset<_N b>::bitset(lon g
unsigned int) [with unsigned int _Nb = 6]'
Compiler says can't convert, but bitset<> has such an explicit

constructor.

No it doesn't. It has a template constructor that takes a string.
Try:

std::bitset<6> b1(string("1010 10"));

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #2
Hunter Hou wrote:
std::bitset<6> b1("101010");


Try:
std::bitset<6> b1(string("1010 10"));
Jul 22 '05 #3
Hunter Hou wrote:
#include <iostream>
#include <bitset>

int main()
{
std::bitset<6>* b1("101010");

std::cout*<<*b1 *<<*std::endl;

return*0;

}


Try

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

int main()
{
std::bitset<6> b1( std::string( "101010" ) );

std::cout << b1 << std::endl;

return 0;

}
Best

Kai-Uwe Bux
Jul 22 '05 #4

"Hunter Hou" <hy***@lucent.c om> wrote in message
news:cb******** @netnews.proxy. lucent.com...
Hello,

I have this very simple program, but it can't be compiled. What's wrong
here?

thanks,
hunter
#include <iostream>
#include <bitset>

int main()
{
std::bitset<6> b1("101010");

std::cout << b1 << std::endl;

return 0;

}
The error is:
bitset.cpp: In function `int main()':
bitset.cpp:8: invalid conversion from `const char*' to `long unsigned int'
bitset.cpp:8: initializing argument 1 of `std::bitset<_N b>::bitset(lon g
unsigned int) [with unsigned int _Nb = 6]'
Compiler says can't convert, but bitset<> has such an explicit constructor.
P.S. > If I use default constructor, everything is fine.


Your compiler is confused, clarify your intentions by first locating the
bitset constructor, if any, that takes a string or char* literal. Its trying
to initialize the bitset container with an unsigned long integer. You could
declare a std::bitset<8> and initialize it with 0x2A or 42 for a binary
sequence of 00101010.

But fortunately, you shouldn't need to, bitset takes a reference to a
std::string as an arguement to one of its constructors:
in <bitset>.... you will find something like this:

explicit bitset(const string& _S, size_t _P = 0, size_t _L = (size_t)(-1))

so try declaring a std::string and initializing it with "101010".

#include <iostream>
#include <bitset>

int main()
{
std::string s1("101010");
std::bitset<6> b1(s1);

std::cout << b1 << std::endl;

return 0;
}

Also:

#include <iostream>
#include <bitset>

int main()
{
std::bitset<8> b1(0x2A); // 00101010

int sz = b1.size();
for (int i = 0; i < sz; i++) // from b1[7] to b1[0]
{
std::cout << b1[sz - i - 1];
}

std::cout << std::endl;

return 0;
}

Jul 22 '05 #5

Your compiler is confused, clarify your intentions by first locating the
bitset constructor, if any, that takes a string or char* literal. Its trying
to initialize the bitset container with an unsigned long integer. You could
declare a std::bitset<8> and initialize it with 0x2A or 42 for a binary
sequence of 00101010.

But fortunately, you shouldn't need to, bitset takes a reference to a
std::string as an arguement to one of its constructors:
in <bitset>.... you will find something like this:

explicit bitset(const string& _S, size_t _P = 0, size_t _L = (size_t)(-1))
Exactly.

so try declaring a std::string and initializing it with "101010".

#include <iostream>
#include <bitset>

int main()
{
std::string s1("101010");
std::bitset<6> b1(s1);

std::cout << b1 << std::endl;

return 0;
}


since string can be defined by const char*, why I have to use a string?
because it's explicit?
Jul 22 '05 #6

"Hunter Hou" <hy***@lucent.c om> wrote in message
news:cb******** @netnews.proxy. lucent.com...

Your compiler is confused, clarify your intentions by first locating the
bitset constructor, if any, that takes a string or char* literal. Its trying to initialize the bitset container with an unsigned long integer. You could declare a std::bitset<8> and initialize it with 0x2A or 42 for a binary
sequence of 00101010.

But fortunately, you shouldn't need to, bitset takes a reference to a
std::string as an arguement to one of its constructors:
in <bitset>.... you will find something like this:

explicit bitset(const string& _S, size_t _P = 0, size_t _L =
(size_t)(-1))
Exactly.

so try declaring a std::string and initializing it with "101010".

#include <iostream>
#include <bitset>

int main()
{
std::string s1("101010");
std::bitset<6> b1(s1);

std::cout << b1 << std::endl;

return 0;
}


since string can be defined by const char*, why I have to use a string?
because it's explicit?


Thats correct, an implicit conversion is not allowed. Its true for a
templated constructor as well since a templated cstor implies the explicit
keyword.

You may find section 9.4 in the following URL usefull:
http://www.icce.rug.nl/documents/cpl...us09.html#l142

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2304
by: Andy Skypeck | last post by:
I have a question concerning the use of the operator in bitset asignment. bitset<4> a("1100"); bitset<4> b("0011"); a = a; Is clearly supported by the standard, and works with all the C++ compilers I have worked with. But what about
3
3721
by: Jason | last post by:
hello, I've been looking into the idea of using a bitset to store a collection of fields efficiently in a data structure that uses a large number of elements. My question is what technique is used to extract subsets of the bitset and use them in normal types? How would i extract bits 3 - 10 inclusive of a bitset and store them in a char, for example, or how could I go about treating some bitset elements as a char value or any other...
2
5365
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is the same. unsigned long long int f; f=3; bitset<64> g(f); cout << f << endl; cout << g << endl; f<<=32;
10
3242
by: halfsearch2 | last post by:
Hi all, I want to sort a bitset vector ( bit 1 > bit 0 ), while it seems bitset doesn't have < or > operation. I tried to use a loop to compare each bit one by one but it's unbearably slow. Any suggestion?
5
4365
by: Rich S. | last post by:
Hi, Is the code below the best way to have the less than function for an 80 bit bitset or is there something faster / better? When I populate this map with millions (... and millions) of sets it gets slower and slower, I'm looking to make it faster. Thanks
14
3018
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
7
8200
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; } v;
2
2717
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
5
2334
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; // convert to bool reference& operator= ( bool x ); // assign from bool reference& operator= ( const reference& x ); // assign from bit reference& flip(); // flip bit value
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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 we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.