在Python返回至少显著位的索引 [英] return index of least significant bit in Python
问题描述
C ++有一组函数,FFS(),民丰金融(),和ffsll(),即返回在给定的二进制整数设置至少显著位
C++ has a set of functions, ffs(), ffsl(), and ffsll(), that return the least significant bit that is set in a given binary integer.
我不知道是否有在Python已经可用的相同功能。我没有看到一个bitarray描述,但也许还有另一种。我希望能避免所有可能的位掩码循环,计算答案,虽然当然,这是不得已的一种选择; FFS()只返回一个整数,我想知道的在Python相媲美的东西。
I'm wondering if there is an equivalent function already available in Python. I don't see one described for bitarray, but perhaps there's another. I am hoping to avoid calculating the answer by looping through all possible bit masks, though of course that's an option of last resort; ffs() simply returns a single integer and I'd like to know of something comparable in Python.
推荐答案
这是可用的 gmpy 包装为GNU多precision库。在我的系统,它是关于4倍比ctypes的解决方案快。
It is available in the gmpy wrapper for the GNU Multi-Precision library. On my system, it is about 4x faster than the ctypes solution.
>>> import gmpy
>>> gmpy.scan1(136)
3
>>> bin(136)
'0b10001000'
这篇关于在Python返回至少显著位的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!