views:

67

answers:

2

So I need to read flags in bits and set flags in bits. These bits are in various sizes of integer: int16, int32, int64, etc.

I would like to have a function that does something like this:

static integertype function(integertype data, char startbit, char endbit);

I don't want to code what will be the same code to isolate bits from for different sizes of integers in separate but identical functions (for the multitude of bit functions I want to write).

I thought about using a void pointer for the data so everything could run through one function. Is this a bad design? What about as far as efficiency goes? I have no concept of bad/good design due to my inexperience.

static int function(void *data, char startbit, char endbit)

These flags have to be looked at very often as this is for a data acquisition system. Would a void pointer implementation be reasonably efficient?

I know premature optimization is bad, but I would like to know what things are generally less or more efficient than others so I can make good decisions.

Thanks in advance for taking me to school.

+3  A: 

Why not make it a templated function?

template<typename T>
static T foobar(T data, char startbit, char endbit);
Thanatos
+4  A: 

Generally, if you need generic functionality you use templates:

template <typename T>
T function(T data, char startbit, char endbit);

But keep in mind we have std::bitset.

GMan
+1 for std::bitset, forgot about that.
Thanatos
Cool guys. I didn't know you could do templated functions in C++, and definitely didn't know about std::bitset.Thanks a lot.
Johnny
This website helped me understand std::bitset and other options. http://www.drdobbs.com/184401382;jsessionid=GTHEKBIV3CLBVQE1GHPSKH4ATMY32JVN
Johnny
@Johnny: Glad to help. Perhaps you might pick up a good C++ book to help solidify and expand your knowledge? http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
GMan