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

Home Posts Topics Members FAQ

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 7341
"Michal Wyrebski" <mi*******@gmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
4765
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 which contains both an array of function pointers: std::vector<_beadGfp> msmtFPs;
6
8831
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* pParam)
1
1388
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 delegate. void Func( (void *)() fn ) { fn(); } void Func2() {}
2
1018
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 DLL. How can I access and pass such a C-function pointer to a C-function out of C#?
17
8579
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. something like this: in my aspx page: <input type=button onClick="doFirstFunction('secondFunctionName');"> then in my .js file:
1
1976
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. function foo2(){} function foo(func){}
18
2880
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 write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
3
4418
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 the front panel class. I kept getting compile error C2276 when I tried passing the address of a member function. After implementing a few changes suggested by the author of the class, I now get the following error when compiling: error C2664:...
40
2371
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 libary do I create a function where user passes this callback function? How would I define the function?
0
10426
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
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
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
7537
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
5430
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
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
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
3
2913
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.