使按位函数适用于任何类型的整数输入 c++ 的最佳方法是什么?
所以我需要以位为单位读取标志并以位为单位设置标志。这些位具有各种大小的整数:int16、int32、int64 等。
我想要一个执行以下操作的函数:
static integertype function(integertype data, char startbit, char endbit);
我不想编写相同的代码来隔离不同大小的位单独但相同的函数中的整数(对于我想编写的多个位函数)。
我考虑过对数据使用一个空指针,这样所有的事情都可以通过一个函数运行。这是一个糟糕的设计吗?就效率而言呢?由于我的经验不足,我对坏/好设计没有概念。
static int function(void *data, char startbit, char endbit)
这些标志必须经常查看,因为这是针对数据采集系统的。 void 指针实现是否相当有效?
我知道过早的优化是不好的,但我想知道哪些事情通常比其他事情效率更低或更高,以便我可以做出正确的决定。
预先感谢您送我去学校。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,如果您需要通用功能,您可以使用模板:
但请记住,我们有
std: :bitset
。Generally, if you need generic functionality you use templates:
But keep in mind we have
std::bitset
.为什么不将其设为模板函数呢?
Why not make it a templated function?