简体   繁体   English

大型二进制字符串的 Python 按位运算

[英]Python bitwise operation on large binary strings

I want to perform bitwise operation on binary strings with length more than 100 zeros and ones.我想对长度超过 100 个零和一个的二进制字符串执行按位运算。 I know i can convert them using something like int('1'*100, 2) which prints 1267650600228229401496703205375L with L character at the end.我知道我可以使用像int('1'*100, 2)这样的东西来转换它们int('1'*100, 2)它会在末尾打印1267650600228229401496703205375LL字符。 Then use python bitwise operators but I think it's a bad idea to convert them into integer.然后使用 python 按位运算符,但我认为将它们转换为整数是个坏主意。 Is there any other way to do this?有没有其他方法可以做到这一点?

I'm guessing that you do not like the idea of using integers because it obfuscates your underlying data.我猜你不喜欢使用整数的想法,因为它混淆了你的基础数据。 Plus it makes it difficult to work with strings that start with '0' (because they trimmed off when converting to an integer), not to mention the subtleties of signed integers and endianness.此外,它很难处理以 '0' 开头的字符串(因为它们在转换为整数时会被修剪掉),更不用说有符号整数和字节序的微妙之处了。

Try using the bitarray module, can be installed with pip: pip install bitarray .尝试使用 bitarray 模块,可以用pip install bitarraypip install bitarray

from bitarray import bitarray
ba1 = bitarray('0' + '1'*100)
ba2 = bitarray('1' + '0'*100)

len(ba1)  # 101
len(ba2)  # 101
ba1[0]    # False
ba2[0]    # True

ba1 | ba2  # bitarray('1111111111.......)

# get your string back
ba1.to01()  # "01111111......."

I can't speak for the efficiency.我不能说效率。 But at least it feels clear as to what you are working with.但至少你对你正在使用的东西感到很清楚。

Also works in python3也适用于python3

Docs: https://pypi.python.org/pypi/bitarray/0.8.1文档: https : //pypi.python.org/pypi/bitarray/0.8.1

这是另一种最佳方法。

bin(int("1100",2)|int("1010", 2))

Do not do bitwise operations on strings of 0 s and 1 s that is plain bad and slow.不要对0 s 和1 s 的字符串进行按位运算,这很糟糕而且很慢。 You shall convert the strings to actual numbers as then the processing of several bits is done at once.您应该将字符串转换为实际数字,然后一次完成几个位的处理。 There is nothing wrong with :没有错

int('1'*128,2) & int('1'*128,2)

But if you already have strings of 0 s and 1 s and you want control over the bitwise operation on parts of the strings.但是,如果您已经有0 s 和1 s 的字符串,并且您希望控制部分字符串的按位运算。 You can do the following:您可以执行以下操作:

l = '1'*1024
r = '1'*1024
print map(lambda x: x[0] & x[1], [(int(l[i:i+64], 2), int(r[i:i+64], 2)) for i in range(0,1024,64)])

This effectively uses the & over small chunks (64 bits) of the string, from left to right.这有效地从左到右使用&在字符串的小块(64 位)上。

You can call bin() on each result and concatenate them back if you need a string.您可以对每个结果调用bin()并在需要字符串时将它们连接回去。 (taking care to remove the starting 0b at the start of the output of bin() ) (注意删除bin()输出开始处的起始0b


python 3 version:蟒蛇3版本:

l = '1'*1024
r = '1'*1024
print(list(map(lambda x: x[0] & x[1], [(int(l[i:i+64], 2), int(r[i:i+64], 2)) for i in range(0,1024,64)])))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM