C++作为数字的位列表
例如,我希望有一个位列表,可以在其中对列表的一部分执行数学运算。
value: 864
as bits, pos: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
as bits, value: 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0
如果我取位 1 到 4 和位 6 到 9,我会得到 0, 1, 1, 0, 1, 0, 0, 0 (104)。然后如果我加上 3,它就变成 0, 1, 1, 0, 1, 0, 1, 1 (107)。
我可以使用 std::vector 和涉及位之间按位运算的过程来完成此操作,代码看起来像这样 if(xor(data[n], data[n2])) {data[n - 1] == false;} 等等...
我什至可以使用 POD 数组,但我在想,C++ 如此接近计算机的内部结构通常没有帮助,但在这里,它可以允许完成此操作非常快。
比如将值存储在一个大 POD 变量或数组中,然后将它们复制到另一个变量的内存中,以便在将它们再次复制回存储位置之前可以完成数学运算?
更好的是,例如加法运算符是否可以以某种方式将内存中的该位置作为参数来直接在数据存储的中间执行加法?
I wish to have a list of bits where I can perform maths operations on a section of the list, for example.
value: 864
as bits, pos: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
as bits, value: 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0
If I take bits 1 to 4 and 6 to 9 I get 0, 1, 1, 0, 1, 0, 0, 0 (104). Then if I add 3 it becomes 0, 1, 1, 0, 1, 0, 1, 1 (107).
I could do this with a std::vector and a process of involving bitwise operations between the bits, with code that looks like this sort of thing if(xor(data[n], data[n2])) {data[n - 1] == false;} etc...
I could even use POD arrays but I was thinking, C++ being so close to the internals of the computer is often not helpfull, but here it is, it could allow this operation to be done extremely quickly.
Something like storing the values in a big POD variable or an array and then copying them into the memory of another variable such that the mathmatical operations could be done before copying them back into the position in there storage again?
Even better could the for example addition operator somehow take that position in memory as a parameter to perform the addition directly into the middle of the store of data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想要 Boost。DynamicBitset:
It sounds like you want Boost.DynamicBitset:
可能的快速方法行不通并且非常有限。
这是完整的解决方案,感谢您提供的信息使我能够完成此任务。可以轻松地对其进行修改以使用 std::vector 代替。
The possible fast way didn't work and would be verry limited.
Here is the full solution, thanks for the info that allowed me to complete this. It can easily be reworked to use std::vector instead.