What's the best way to make a bitwise function work for any type of integer input c++?
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 fo开发者_如何学运维r taking me to school.
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
.
Why not make it a templated function?
template<typename T>
static T foobar(T data, char startbit, char endbit);
精彩评论