C++作为数字的位列表

发布于 2024-12-05 10:50:36 字数 605 浏览 1 评论 0原文

例如,我希望有一个位列表,可以在其中对列表的一部分执行数学运算。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-12-12 10:50:36

听起来您想要 BoostDynamicBitset

dynamic_bitset 类表示一组位。它通过 operator[] 提供对各个位的值的访问,并提供可应用于内置整数的所有按位运算符,例如 operator& 和 >运算符<<。该集合中的位数是在运行时通过 dynamic_bitset 构造函数的参数指定的。


It sounds like you want Boost.DynamicBitset:

The dynamic_bitset class represents a set of bits. It provides accesses to the value of individual bits via an operator[] and provides all of the bitwise operators that one can apply to builtin integers, such as operator& and operator<<. The number of bits in the set is specified at runtime via a parameter to the constructor of the dynamic_bitset.

檐上三寸雪 2024-12-12 10:50:36

可能的快速方法行不通并且非常有限。

unsigned int n = 8;
unsigned short *p1, *p2;
p1 = reinterpret_cast<unsigned short*>(&n);
p2 = reinterpret_cast<unsigned short*>((&n)+1);
std::cout<<*p1<<", "<<*p2<<std::endl;

这是完整的解决方案,感谢您提供的信息使我能够完成此任务。可以轻松地对其进行修改以使用 std::vector 代替。

#include <math.h> // for pow in weighted_random_UINT
#include <bitset> // or std::vector<bool>

template <std::size_t T> // this line is not required with std::vector
unsigned int get_chunk(std::bitset<T>* btset, unsigned int start, unsigned int end)
{
    unsigned long out = 0;
    for(unsigned int n = start; n < end; n++)
    {
        out += unsigned long((*btset)[n] * pow(2,double(n-start)));
    }
    return out;
}

void main()
{
    // 6 bits of data in reverse order
    std::bitset<6> data (std::string("000110"));

    // all the data numerically
    std::cout<<get_chunk<6>(&data,0,5)<<std::endl;

    // the first 3 bits as a number
    std::cout<<get_chunk<6>(&data,0,2)<<std::endl;

    // the last 3 bits as a number
    std::cout<<get_chunk<6>(&data,3,5)<<std::endl;
}

The possible fast way didn't work and would be verry limited.

unsigned int n = 8;
unsigned short *p1, *p2;
p1 = reinterpret_cast<unsigned short*>(&n);
p2 = reinterpret_cast<unsigned short*>((&n)+1);
std::cout<<*p1<<", "<<*p2<<std::endl;

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.

#include <math.h> // for pow in weighted_random_UINT
#include <bitset> // or std::vector<bool>

template <std::size_t T> // this line is not required with std::vector
unsigned int get_chunk(std::bitset<T>* btset, unsigned int start, unsigned int end)
{
    unsigned long out = 0;
    for(unsigned int n = start; n < end; n++)
    {
        out += unsigned long((*btset)[n] * pow(2,double(n-start)));
    }
    return out;
}

void main()
{
    // 6 bits of data in reverse order
    std::bitset<6> data (std::string("000110"));

    // all the data numerically
    std::cout<<get_chunk<6>(&data,0,5)<<std::endl;

    // the first 3 bits as a number
    std::cout<<get_chunk<6>(&data,0,2)<<std::endl;

    // the last 3 bits as a number
    std::cout<<get_chunk<6>(&data,3,5)<<std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文