英文:
How to iterate over each bit of a byte in Python?
问题
让我们假设 a=0xF0
,b=[a的位列表]
。我如何迭代遍历a
的每个位,以便我可以执行位运算,将该位与列表b
中的位进行位运算?
我已尝试使用bin()
方法将a
转换为其二进制形式,然后循环遍历它,但由于a
的每个位现在都是字符串数据类型,因此无法再执行位运算。
任何帮助都将不胜感激!
英文:
Let's say a=0xF0
and b=[a list of bits]
. How do I iterate over each bit of a
so that i can perform a bitwise operation between that bit and a bit in list b
?
I have tried converting a
to its binary form using the bin()
method before looping through it, however, i can no longer perform a bitwise operation as each bit of a
is now of the string datatype.
Any help is appreciated!
答案1
得分: 0
你可以使用位移操作:
a = 0xF0
b = [1, 0, 1, 0, 1, 0, 1, 0]
print(bin(a))
# 0b11110000
for idx, bit_in_b in enumerate(b):
bit_in_a = (a >> idx) & 1
binary_and_operation = bit_in_a & bit_in_b
print(bit_in_a, binary_and_operation)
# 0 0
# 0 0
# 0 0
# 0 0
# 1 1
# 1 0
# 1 1
# 1 0
注意两点:
a
从右到左迭代,b
从左到右迭代,但根据需要可以更改。- 如果
len(b)
不等于a
中的比特数,行为可能不是你想要的。
英文:
You can use bitshift:
a = 0xF0
b = [1, 0, 1, 0, 1, 0, 1, 0]
print(bin(a))
# 0b11110000
for idx, bit_in_b in enumerate(b):
bit_in_a = (a >> idx) & 1
binary_and_operation = bit_in_a & bit_in_b
print(bit_in_a, binary_and_operation)
# 0 0
# 0 0
# 0 0
# 0 0
# 1 1
# 1 0
# 1 1
# 1 0
Careful to 2 points:
a
will be iterated from right to left and b from left to right, but you can change that as needed.- if
len(b)
!= number of bits ina
, behavior might not be what you want.
答案2
得分: 0
一种简短的表达方式如下:
```python
bin(int("".join([str(bit) for bit in b]), 2) ^ a)[2:].zfill(max(len(b), (len(bin(a)) - 2)))
它的工作原理如下:
"".join([str(bit) for bit in b])
将您的列表转换为字符串int(..., 2)
转换为二进制数字^
是按位异或运算(或者任何其他您想要的操作)bin(...)
将其转换回二进制表示[2:]
去除字符串的0b
部分.zfil(...)
填充结果的零,直到结果具有与两个输入中较大者相同数量的位数,以防位运算结果以零开头。
示例:
a = 0xF0
b = [0, 1, 1, 1, 0, 0, 1, 0]
result = bin(int("".join([str(bit) for bit in b]), 2) ^ a)[2:].zfill(max(len(b), (len(bin(a)) - 2)))
print(result)
输出:
10000010
<details>
<summary>英文:</summary>
A one-liner would be this:
bin(int("".join([str(bit) for bit in b]), 2) ^ a)[2:].zfill(max(len(b), (len(bin(a))-2)))
It works like so:
- `"".join([str(bit) for bit in b]` converts your list to a string
- `int(..., 2)` converts to a number in base 2
- `^` is the bitwise `xor` (or any other operation you want)
- `bin(...)` converts it back to a binary representation
- `[2:]` drops the `0b` part of the string
- `.zfil(...)` pads the result with zeroes until the result has the same number of bits as the largest of the two inputs, in case the bitwise result starts with zeroes.
Example:
a = 0xF0
b = [0, 1, 1, 1, 0, 0, 1, 0]
result = bin(int("".join([str(bit) for bit in b]), 2) ^ a)[2:].zfill(max(len(b), (len(bin(a))-2)))
print(result)
Output:
10000010
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论