Tôi có một tệp trong đó byte đầu tiên chứa thông tin được mã hóa. Trong MATLAB, tôi có thể đọc Byte Bit từng bit bằng
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
5, sau đó lấy từng bit bằng x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
6, v.v.Có bất kỳ đầu đọc bit tương đương trong Python?
Hỏi ngày 5 tháng 4 năm 2010 lúc 2:57Apr 5, 2010 at 2:57
Đọc các bit từ một tập tin, các bit thấp đầu tiên.
def bits[f]:
bytes = [ord[b] for b in f.read[]]
for b in bytes:
for i in xrange[8]:
yield [b >> i] & 1
for b in bits[open['binary-file.bin', 'r']]:
print b
Đã trả lời ngày 5 tháng 4 năm 2010 lúc 8:22Apr 5, 2010 at 8:22
3
Đơn vị nhỏ nhất bạn có thể làm việc là một byte. Để làm việc ở cấp độ bit, bạn cần sử dụng các toán tử bitwise.
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
Đã trả lời ngày 5 tháng 4 năm 2010 lúc 2:58Apr 5, 2010 at 2:58
Brian R. Bondybrian R. BondyBrian R. Bondy
332K122 Huy hiệu vàng587 Huy hiệu bạc629 Huy hiệu đồng122 gold badges587 silver badges629 bronze badges
3
Bạn sẽ không thể đọc từng bit một - bạn phải đọc nó byte bởi byte. Bạn có thể dễ dàng trích xuất các bit ra, mặc dù:
f = open["myfile", 'rb']
# read one byte
byte = f.read[1]
# convert the byte to an integer representation
byte = ord[byte]
# now convert to string of 1s and 0s
byte = bin[byte][2:].rjust[8, '0']
# now byte contains a string with 0s and 1s
for bit in byte:
print bit
Đã trả lời ngày 5 tháng 4 năm 2010 lúc 3:30Apr 5, 2010 at 3:30
Daniel Gdaniel gDaniel G
64.5K7 Huy hiệu vàng41 Huy hiệu bạc41 Huy hiệu đồng7 gold badges41 silver badges41 bronze badges
6
Tham gia một số câu trả lời trước đây tôi sẽ sử dụng:
[int[i] for i in "{0:08b}".format[byte]]
Đối với mỗi byte đọc từ tệp. Kết quả cho một ví dụ byte 0x88 là:
>>> [int[i] for i in "{0:08b}".format[0x88]]
[1, 0, 0, 0, 1, 0, 0, 0]
Bạn có thể gán nó cho một biến và làm việc theo yêu cầu ban đầu của bạn. "{0,08}" là để đảm bảo chiều dài byte đầy đủ
Đã trả lời ngày 3 tháng 1 năm 2017 lúc 5:15Jan 3, 2017 at 5:15
FranciscofranciscoFrancisco
Huy hiệu bạc 1311 Bạc7 Huy hiệu đồng1 silver badge7 bronze badges
Để đọc một byte từ một tập tin:
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
7. Lưu ý: Tệp được mở ở chế độ nhị phân.Để có được bit, hãy chuyển đổi bytestring thành một số nguyên:
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
8 [Python 3] hoặc x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
9 [Python 2] và trích xuất bit mong muốn: f = open["myfile", 'rb']
# read one byte
byte = f.read[1]
# convert the byte to an integer representation
byte = ord[byte]
# now convert to string of 1s and 0s
byte = bin[byte][2:].rjust[8, '0']
# now byte contains a string with 0s and 1s
for bit in byte:
print bit
0:>>> for i in range[8]: [b'a'[0] >> i] & 1
...
1
0
0
0
0
1
1
0
>>> bin[b'a'[0]]
'0b1100001'
Đã trả lời ngày 12 tháng 5 năm 2016 lúc 14:16May 12, 2016 at 14:16
JFSJFSjfs
385K183 Huy hiệu vàng949 Huy hiệu bạc1623 Huy hiệu Đồng183 gold badges949 silver badges1623 bronze badges
Có hai cách có thể để trả về bit thứ i của một byte. "Bit đầu tiên" có thể đề cập đến bit thứ tự cao hoặc nó có thể đề cập đến bit thứ tự thấp hơn.
Dưới đây là một hàm lấy chuỗi và chỉ mục làm tham số và trả về giá trị của bit tại vị trí đó. Như đã viết, nó coi bit thứ tự thấp là bit đầu tiên. Nếu bạn muốn bit thứ tự cao đầu tiên, chỉ cần giải phóng dòng được chỉ định.
def bit_from_string[string, index]:
i, j = divmod[index, 8]
# Uncomment this if you want the high-order bit first
# j = 8 - j
if ord[string[i]] & [1 >> for i in range[8]:
>>> print i, bit_from_string['\x04', i]
0 0
1 0
2 1
3 0
4 0
5 0
6 0
7 0
Bây giờ, cho cách nó hoạt động:
Một chuỗi bao gồm các byte 8 bit, vì vậy trước tiên chúng tôi sử dụng divmod [] để chia chỉ mục thành các phần:
2: Chỉ mục của byte chính xác trong chuỗif = open["myfile", 'rb'] # read one byte byte = f.read[1] # convert the byte to an integer representation byte = ord[byte] # now convert to string of 1s and 0s byte = bin[byte][2:].rjust[8, '0'] # now byte contains a string with 0s and 1s for bit in byte: print bit
3: Chỉ số của bit chính xác trong byte đóf = open["myfile", 'rb'] # read one byte byte = f.read[1] # convert the byte to an integer representation byte = ord[byte] # now convert to string of 1s and 0s byte = bin[byte][2:].rjust[8, '0'] # now byte contains a string with 0s and 1s for bit in byte: print bit
Chúng tôi sử dụng hàm
f = open["myfile", 'rb']
# read one byte
byte = f.read[1]
# convert the byte to an integer representation
byte = ord[byte]
# now convert to string of 1s and 0s
byte = bin[byte][2:].rjust[8, '0']
# now byte contains a string with 0s and 1s
for bit in byte:
print bit
4 để chuyển đổi ký tự ở f = open["myfile", 'rb']
# read one byte
byte = f.read[1]
# convert the byte to an integer representation
byte = ord[byte]
# now convert to string of 1s and 0s
byte = bin[byte][2:].rjust[8, '0']
# now byte contains a string with 0s and 1s
for bit in byte:
print bit
5 thành loại số nguyên. Sau đó, f = open["myfile", 'rb']
# read one byte
byte = f.read[1]
# convert the byte to an integer representation
byte = ord[byte]
# now convert to string of 1s and 0s
byte = bin[byte][2:].rjust[8, '0']
# now byte contains a string with 0s and 1s
for bit in byte:
print bit
6 tính giá trị của bit thứ J bằng cách chuyển bên trái 1 bằng f = open["myfile", 'rb']
# read one byte
byte = f.read[1]
# convert the byte to an integer representation
byte = ord[byte]
# now convert to string of 1s and 0s
byte = bin[byte][2:].rjust[8, '0']
# now byte contains a string with 0s and 1s
for bit in byte:
print bit
3. Cuối cùng, chúng tôi sử dụng bitwise-và để kiểm tra xem bit đó được đặt. Nếu vậy hãy trả lại 1, nếu không thì trả về 0.Đã trả lời ngày 5 tháng 4 năm 2010 lúc 3:52Apr 5, 2010 at 3:52
Daniel Stutzbachdaniel StutzbachDaniel Stutzbach
71.8K17 Huy hiệu vàng85 Huy hiệu bạc76 Huy hiệu đồng17 gold badges85 silver badges76 bronze badges
1
Giả sử bạn có một tệp có tên bloom_filter.bin chứa một mảng các bit và bạn muốn đọc toàn bộ tệp và sử dụng các bit đó trong một mảng.
Đầu tiên tạo ra mảng nơi các bit sẽ được lưu trữ sau khi đọc,
from bitarray import bitarray
a=bitarray[size] #same as the number of bits in the file
Mở tệp, sử dụng mở hoặc với, mọi thứ đều ổn ... Tôi đang mở với Mở ở đây,
f=open['bloom_filter.bin','rb']
Bây giờ tải tất cả các bit vào mảng 'A' tại một lần sử dụng,
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
0'A' bây giờ là một Bitarray chứa tất cả các bit
Đã trả lời ngày 15 tháng 5 năm 2015 lúc 8:34May 15, 2015 at 8:34
TaruntarunTarun
2.4291 Huy hiệu vàng12 Huy hiệu bạc7 Huy hiệu đồng1 gold badge12 silver badges7 bronze badges
2
Điều này là khá nhanh, tôi sẽ nghĩ:
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
1Đã trả lời ngày 6 tháng 11 năm 2014 lúc 19:44Nov 6, 2014 at 19:44
vitiralvitirusvitiral
7.7675 Huy hiệu vàng25 Huy hiệu bạc40 Huy hiệu đồng5 gold badges25 silver badges40 bronze badges
Tôi nghĩ rằng đây là một cách pythonic hơn:
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
2Kết quả của khối này là:
'10001100'
Tôi đã có được các mặt phẳng bit của hình ảnh và chức năng này đã giúp tôi viết khối này:
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
3Cũng bởi khối này, tôi đã có thể tạo hình ảnh nguyên thủy từ các máy bay bit được trích xuất
x = 3
#Check if the 1st bit is set:
x&1 != 0
#Returns True
#Check if the 2nd bit is set:
x&2 != 0
#Returns True
#Check if the 3rd bit is set:
x&4 != 0
#Returns False
4Đã trả lời ngày 5 tháng 3 lúc 0:55Mar 5 at 0:55