如何在没有按位操作的情况下从字节中获取位

How to get a bit from a byte without bitwise operations

我想知道是否有一种方法可以从字节中获取特定位,而无需按位运算符。

我想我找到了线索,我想出了一种用平方根或幂来计算的方法……mod。

先谢谢你。

使用 Python 的示例:

>>> def getbit(i, n):
...     return i // 2 ** n % 2
... 
>>> for i in range(8): print(f"Bit {i}: {getbit(0b10101111, i)}")
... 
Bit 0: 1
Bit 1: 1
Bit 2: 1
Bit 3: 1
Bit 4: 0
Bit 5: 1
Bit 6: 0
Bit 7: 1