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

Home Posts Topics Members FAQ

initializing bitset with variable number of bits

The subject isnt very clear, but ill do my best to explain.

In a class in need a bitset which size is defined by the constructor,
e.g. something like this:

class Example {
std::bitset<?>B s;
public:
Example(int input) {
? = input;
}
};

Now, my knowledge in c++ is simply to limited to work around this. I
know that in order to use a variable to initialize a bitset, it has to
be constant, but allso the constant variable has to be initialized
imidiadly, and ofcourse it all has to be done in the right order,
otherwise nothing will make sence or work for that matter. Im confused
and i hope you canhelp.

Regards

May 11 '07 #1
6 6394
za******@gmail. com wrote:
The subject isnt very clear, but ill do my best to explain.

In a class in need a bitset which size is defined by the constructor,
e.g. something like this:

class Example {
std::bitset<?>B s;
Since the size of the bitset is part of its _type_ definition, bitsets
with different sizes would mean that your 'Example' would have members
of different types. That means those are different 'Example' _types_.
You can only achieve that by making 'Example' a template.
public:
Example(int input) {
? = input;
}
};

Now, my knowledge in c++ is simply to limited to work around this.
Actually, C++ is simply limited, not your knowledge. C++ does not
allow to have the same _class_ to contain different types of members
depending on some run-time condition.
I
know that in order to use a variable to initialize a bitset, it has to
be constant, but allso the constant variable has to be initialized
imidiadly, and ofcourse it all has to be done in the right order,
otherwise nothing will make sence or work for that matter. Im confused
and i hope you canhelp.
template<size_t n>
class Example {
std::bitset<nBs ;
public:
...
};

....
Example<12e12;
....

Now, _types_ have to be fixed at the compile time. You cannot create
types during run-time. That's the main limitation.

Perhaps you can roll your own "bitset", instead of using the standard
template... Think 'std::vector<bo ol>', which has dynamic _size_.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 11 '07 #2
On 12 Maj, 00:23, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
zacar...@gmail. com wrote:
The subject isnt very clear, but ill do my best to explain.
In a class in need a bitset which size is defined by the constructor,
e.g. something like this:
class Example {
std::bitset<?>B s;

Since the size of the bitset is part of its _type_ definition, bitsets
with different sizes would mean that your 'Example' would have members
of different types. That means those are different 'Example' _types_.
You can only achieve that by making 'Example' a template.
public:
Example(int input) {
? = input;
}
};
Now, my knowledge in c++ is simply to limited to work around this.

Actually, C++ is simply limited, not your knowledge. C++ does not
allow to have the same _class_ to contain different types of members
depending on some run-time condition.
I
know that in order to use a variable to initialize a bitset, it has to
be constant, but allso the constant variable has to be initialized
imidiadly, and ofcourse it all has to be done in the right order,
otherwise nothing will make sence or work for that matter. Im confused
and i hope you canhelp.

template<size_t n>
class Example {
std::bitset<nBs ;
public:
...

};

...
Example<12e12;
...

Now, _types_ have to be fixed at the compile time. You cannot create
types during run-time. That's the main limitation.

Perhaps you can roll your own "bitset", instead of using the standard
template... Think 'std::vector<bo ol>', which has dynamic _size_.
I thought vector<boolwas obsolete, but it might the solution,
however it is my experience, and i might be wrong, that vector<bool>
uses one byte of memory per bool, and if that the case it is
definently not the solution.

Anyhow, thanks for now.

May 11 '07 #3
I thought vector<boolwas obsolete...

I was of course thinking of the bitvector ot bit_vector? cant
remember, anyway i still seem to remember that vector<booltake s up
one byte of memory per bit, which, as i wrote before, is a problem.

May 11 '07 #4
za******@gmail. com wrote:
:: On 12 Maj, 00:23, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
::: zacar...@gmail. com wrote:
:::: The subject isnt very clear, but ill do my best to explain.
:::
:::: In a class in need a bitset which size is defined by the
:::: constructor, e.g. something like this:
:::
:::: class Example {
:::: std::bitset<?>B s;
:::
::: Since the size of the bitset is part of its _type_ definition,
::: bitsets with different sizes would mean that your 'Example' would
::: have members of different types. That means those are different
::: 'Example' _types_. You can only achieve that by making 'Example' a
::: template.
:::
:::: public:
:::: Example(int input) {
:::: ? = input;
:::: }
:::: };
:::
:::: Now, my knowledge in c++ is simply to limited to work around this.
:::
::: Actually, C++ is simply limited, not your knowledge. C++ does not
::: allow to have the same _class_ to contain different types of members
::: depending on some run-time condition.
:::
:::: I
:::: know that in order to use a variable to initialize a bitset, it
:::: has to be constant, but allso the constant variable has to be
:::: initialized imidiadly, and ofcourse it all has to be done in the
:::: right order, otherwise nothing will make sence or work for that
:::: matter. Im confused and i hope you canhelp.
:::
::: template<size_t n>
::: class Example {
::: std::bitset<nBs ;
::: public:
::: ...
:::
::: };
:::
::: ...
::: Example<12e12;
::: ...
:::
::: Now, _types_ have to be fixed at the compile time. You cannot
::: create types during run-time. That's the main limitation.
:::
::: Perhaps you can roll your own "bitset", instead of using the
::: standard template... Think 'std::vector<bo ol>', which has dynamic
::: _size_.
:::
:: I thought vector<boolwas obsolete, but it might the solution,
:: however it is my experience, and i might be wrong, that vector<bool>
:: uses one byte of memory per bool, and if that the case it is
:: definently not the solution.
::
:: Anyhow, thanks for now.

No, vector<booluses one bit per element, which IS its problem. This might
be good for you, but in general we would expect a vector<boolto store
bools.
Bo Persson
May 12 '07 #5
In a class in need a bitset which size is defined by the constructor,
e.g. something like this:

class Example {
std::bitset<?>B s;
public:
Example(int input) {
? = input;
}

};
Perhaps you can try dynamic_bitset in boost library.
http://www.boost.org/libs/dynamic_bi...ic_bitset.html

May 12 '07 #6

<za******@gmail .comwrote in message
news:11******** **************@ e51g2000hsg.goo glegroups.com.. .
I thought vector<boolwas obsolete...

I was of course thinking of the bitvector ot bit_vector? cant
remember, anyway i still seem to remember that vector<booltake s up
one byte of memory per bit, which, as i wrote before, is a problem.
Here is a little snippet I was playing with. Maybe it will give you an idea.
(You seem 'hep' enough to figure out the #includes. <G>)

{ // main or func
using std::cout; // for NG posts
bool bl(true);
std::vector<std ::bitset<1 VecBit( 7, 1 );
// note 'bitset' does not have the '.at()' member.
if( bl == VecBit.at(0)[0] ){
cout<<"VecBit.a t(0)="<<VecBit. at(0)<<std::end l;
cout<<" ="<<std::boolal pha<<VecBit.at( 0)[0]
<<std::endl;
}
// out: VecBit.at(0)=1
// out: =true

// Example (stl docs)
std::bit_vector V(5);
V[0] = true;
V[1] = false;
V[2] = false;
V[3] = true;
V[4] = false;
for(std::bit_ve ctor::iterator i( V.begin() ); i < V.end(); ++i){
cout<<" "<<std::boolalp ha<<(*i)<<std:: endl;
}
cout <<std::endl;
}

Of course, 'vector<bitset< ' is WAY more inefficient than 'vector<bool>',
but I left it in there just to show it could be done. <G>
'std::bit_vecto r' sounds like what you want.

--
Bob R
POVrookie
May 12 '07 #7

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

Similar topics

5
5471
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(): "...each character is 1 if the corresponding bit is set, and 0 if it is not. In general, character position i corresponds to bit position N - 1 -
2
7302
by: Michal Wyrebski | last post by:
Hello, I was thinking how is the fastest and the best way of passing bitset variable to some function. Only one method came to my mind. It's about passing converted value: #v+ void func(unsigned long UL) { bitset<8> B(UL); cout << "B: " << B << endl;
2
1344
by: ma740988 | last post by:
Given an unsigned int variable that's part of a composite type struct test { unsigned int mask; }; Assume 4 bytes for unsigned int. In one function, I'd like to set any bit or combination of bits between bits 0 and 23 for the parameter mask. So lets suppose I set bits 5, 15 and 23 of mask.
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;
10
2298
by: woessner | last post by:
I'm using a C++ compiler on a DSP that doesn't provide the bitset class. Can anyone suggest a replacement for it? I've written a lot of code that uses the bitset class (before I knew about the... inadequacies of the compiler), so it needs to have approximately the same interface. Thanks in advance, Bill
4
2483
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template Library container. Actaully what's so special in STL containers? What I know is that there will be certain operators overloaded, supports template meta programming, having iterators, compatible with
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
2124
by: arnuld | last post by:
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. */
1
6012
by: Mothra | last post by:
.... is that it does not return the length of the bit set. For example, if I create a new 8-bit set: BitSet eightBits = new BitSet(8); Running eightBits.length() returns zero, rather than eight. The problem is that I want to be able to convert the bit set back to an integer at some point. I thought the easiest way would be to iterate
0
8397
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
8827
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
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6167
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
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
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
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.