473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing bitset to function

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;
}

int main() {
bitset<8> H('A');
func(H.to_ulong() );
return 0;
}
#v-

Of course converting to string is also possible.

OK this just works. But what if I want to pass some number of bits and
func() had no information about its number?
It's impossible to write like that:

#v+
void func(unsigned long UL, const int len) {
bitset<len> B(UL);
(...)
#v-

g++ says: `len' cannot appear in a constant-expression

How, then, pass some number of bits, that function could create
stucture (bitset) of required length?
Any ideas?
Michal

Oct 12 '05 #1
2 7254
"Michal Wyrebski" <mi*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I was thinking how is the fastest and the best way of passing bitset
variable to some function.


Best way is probably passing by reference. Very common... To determine
whether it's the fastest, you will need to test different approaches in your
program.

#include <iostream>
#include <bitset>

typedef std::bitset<8> EightBits;

void func(EightBits const & bits)
{
std::cout << bits << '\n';
}

int main()
{
EightBits h('A');
func(h);
}

Ali

Oct 13 '05 #2
Michal Wyrebski wrote:
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;
}

int main() {
bitset<8> H('A');
func(H.to_ulong() );
return 0;
}
#v-

Of course converting to string is also possible.

OK this just works. But what if I want to pass some number of bits and
func() had no information about its number?
It's impossible to write like that:

#v+
void func(unsigned long UL, const int len) {
bitset<len> B(UL);
(...)
#v-

g++ says: `len' cannot appear in a constant-expression

How, then, pass some number of bits, that function could create
stucture (bitset) of required length?
Cannot be done, because bitset is a template the size must be a compile
time constant.
Any ideas?


If you need a set of bits whose size is not determined at compile time
then vector<bool>, vector<char> and string are all possiblities.
vector<bool> is specialised to take the minimum possible space (i.e.
several bits are stored as a single byte).

john
Oct 13 '05 #3

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

Similar topics

3
by: glen stark | last post by:
Hi all. I'm working with an array of member function pointers (they are all get function of the class Bead). The typedef is: typedef _real (Bead::*_beadGfp)(void); I have a class System...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
1
by: Peter | last post by:
Hello, Thanks for reviewing my question. I am coming from a C++ background and would like to know how do you pass a function as a parameter to a function in C#? I am assume you must use a...
2
by: MrPugh | last post by:
Hi! I have a strange problem: I need to wrap a C-function in a native DLL into a C#-class. This function expects one C-function pointer out of a set of some C-functions defined in the same...
17
by: Kevin Blount | last post by:
I have a system that I want to try, which requires me, from an aspx page, to call a generic function to work perform one task, then i want to call another function with the results of that task. ...
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
3
by: ifitzgerald | last post by:
I am attempting to use a wrapper class to the Win32 timer API in a Visual C++ 6.0 MFC application. The callback function needs to have access to the front panel, and as such needs to be a member of...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.