获得C++的最低设置的最快方法是什么。 std :: bitset?
我注意到 std :: bitset 没有用于返回或弹出的函数位置的最低集合(也不是最高的集合位)。什么是完成此任务的最快方法,特别是对于STD :: BITSET对象,不能保证为一定数量的位长吗?我在G ++编译器扩展程序中查看, c ++ 20 numerics numerics 找到与我的问题有关的任何东西。
要考虑的两种明显的方法将是在比特斯的长度上循环循环,并使用operator []
或使用operator>>>
逐渐移动位置直至第一个集合位置为成立。
I noticed that std::bitset does not have a function for returning or popping the lowest set bit of a bitset (nor the highest set bit for that matter). What is the fastest way to accomplish this task, specifically for std::bitset objects not guaranteed to be a certain number of bits long? I looked in the g++ compiler extensions and C++20 numerics library and didn't find anything related to my problem.
Two obvious methods to consider would be looping over the length of the bitset and using the operator[]
or gradually shifting the bitset using operator>>
until the first set bit is found.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像大多数现代实现一样,我将使用尾随零,以使其易于处理没有设置的情况。要利用硬件计数落后指令,我们可以使用
to_ullong()
,但是要使它起作用,我们需要掩盖该值以使其适合无符号>未签名的长长
Demo on Godbolt< /a>
以这种方式,不需要对单个位进行循环,并且可以使用硬件加速度。但这仍然不是最有效的方法,因为每次迭代仍然需要掩盖整个bitset。这就是为什么
std :: bitset
不是一般操作的好工具,而我总是在实践中避免使用它们。将阵列包装钻头操作的课程在性能和灵活性方面会更好I'm going to use trailing zero like most modern implementations to make it easy to deal with the case where there's no set bit. To utilize the hardware count trailing zero instruction we can use
to_ullong()
, but to make it work we'll need to mask the value to make it fit in anunsigned long long
Demo on Godbolt
This way no looping over individual bits is required and hardware acceleration can be used. But it's still not the most efficient method because each iteration the whole bitset still needs to be masked off. That's why
std::bitset
isn't a good tool for operating on bits in general and I always avoid them in practice. A class wrapping an array for bit operations will be much better in performance and flexibility没有
.lsb()
或.msb()
成员函数,但是std :: bitset
确实提供.size()在“> @phuctv 用于
.count()),您可以在其中构造LSB和MSB例程。.yany()
假设有效的
std :: bitset
您可以使用.yany()()
(或仅检查未符号值),验证至少一个位是TRUE设置的。验证至少一个位是正确的,只需从bit-0
循环到bit-(bitset.size() - 1)
使用检查设置位.test()
获得LSB。然后,只需在相同的测试中反向循环以找到MSB。简短的实现将是:
示例使用/输出
扩展了std :: bitset并添加
.lsb()
and.msb()
成员函数除了简单地编写几个功能外,您还可以从
std :: bitset
添加.lsb()
和。 MSB()
成员函数到派生类。使用以上相同实现的简短类声明可能是:
然后您可以直接使用
.lsb()
和.msb()
成员,例如(相同的输出)
There is no
.lsb()
or.msb()
member functions, butstd::bitset
does provide.size()
and.test()
(and.any()
, credit to @phuctv for use of.any()
over.count()
) with which you can construct the lsb and msb routines.Presuming a valid
std::bitset
you can verify that at least one bit is set true using.any()
(or just check the unsigned value). After verifying at least one bit is true, simply loop frombit-0
tobit-(bitset.size() - 1)
checking for a set bit with.test()
to obtain the LSB. Then just loop in reverse with the same test to find the MSB.A short implementation would be:
Example Use/Output
Extend std::bitset and Add
.lsb()
and.msb()
Member FunctionsIn addition to simply writing a couple of functions, you can just derive from
std::bitset
and add.lsb()
and.msb()
member functions to the derived class.A short class declaration using the same implementations above could be:
Then you can simply use the
.lsb()
and.msb()
members directly, e.g.(same output)
有点较晚的答案,C ++ 20包括
&lt; /代码>
具有您想要的功能。
特别是
std :: countr_zero
连续0位的数量从最小显着的位开始。A bit of a late answer, C++20 includes
<bit>
which has the functions you want.In particular
std::countr_zero
which returns the number of consecutive 0 bits starting from the least significant bit.您可以做这样的事情:
You can do something like this: