Python bekerja dengan bilangan biner

Bilangan Biner

Operator Biner, Byte, dan Bitwise dengan Python
09. 36

Tandai sebagai Selesai

Bahan Pendukung

Slide Kursus Tutorial yang Direkomendasikan (. pdf) Contoh Kode (. zip)

Jadilah Anggota untuk bergabung dalam percakapan

Catatan. Ini bukan pengkodean karakter, itu datang nanti. Ini hanyalah cara untuk melihat satu set 1 dan 0 dan melihatnya dalam tiga cara berbeda (atau sistem angka)

Contoh.  

Input : 10011011 
 
Output :
1001 1011 ---- 9B (in hex)
1001 1011 ---- 155 (in decimal)
1001 1011 ---- 233 (in octal)

Ini jelas menunjukkan bahwa serangkaian bit dapat diinterpretasikan secara berbeda dengan cara yang berbeda. Kami sering menggunakan representasi hex dari sebuah byte daripada biner karena lebih pendek untuk ditulis, ini hanya representasi dan bukan interpretasi

Pengkodean

Sekarang kita tahu apa itu byte dan seperti apa bentuknya, mari kita lihat bagaimana itu diinterpretasikan, terutama dalam string. Pengkodean Karakter adalah cara untuk menetapkan nilai ke byte atau kumpulan byte yang mewakili karakter tertentu dalam skema itu. Beberapa pengkodean adalah ASCII (mungkin yang tertua), Latin, dan UTF-8 (paling banyak digunakan saat ini. Dalam arti tertentu, pengkodean adalah cara komputer untuk merepresentasikan, mengirim, dan menafsirkan karakter yang dapat dibaca manusia. Ini berarti bahwa sebuah kalimat dalam satu penyandian mungkin menjadi sama sekali tidak dapat dipahami dalam penyandian lainnya

Python dan Byte

Dari sudut pandang pengembang, perubahan terbesar di Python 3 adalah penanganan string. Di Python 2, tipe str digunakan untuk dua jenis nilai yang berbeda - teks dan byte, sedangkan di Python 3, ini adalah tipe yang terpisah dan tidak kompatibel. Ini berarti bahwa sebelum Python3 kita dapat memperlakukan sekumpulan byte sebagai string dan bekerja dari sana, sekarang tidak demikian, sekarang kita memiliki tipe data terpisah, yang disebut byte. Tipe data ini dapat dijelaskan secara singkat sebagai string byte, yang pada dasarnya berarti, setelah tipe data byte diinisialisasi, tipe data tersebut tidak dapat diubah.  

Contoh

Python3




bytestr=

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
0
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
1
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
2

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_4

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_5

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
7

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_9
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
0
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
1

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
3
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
0
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
5=
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
7

Keluaran.  

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_

Bytestring adalah apa yang dikatakannya hanyalah serangkaian byte, misalnya '©?

b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'

Ini menimbulkan masalah lain, kita perlu mengetahui pengkodean string biner, karena string yang sama dalam pengkodean lain (latin-1) terlihat berbeda

© ð â

Contoh

Python3




b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
9
© ð â
0
© ð â
1
© ð â
2
© ð â
3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
9
© ð â
0
© ð â
1
© ð â
8
© ð â
3

Keluaran

Python bekerja dengan bilangan biner

Seperti yang terlihat di atas dimungkinkan untuk menyandikan atau mendekode string dan string biner menggunakan fungsi encode() atau decode(). Kami membutuhkan pengkodean karena dalam beberapa pengkodean tidak mungkin untuk memecahkan kode string. Masalah ini bertambah jika tidak menggunakan karakter non Latin seperti Ibrani, Jepang, dan Cina. Karena dalam bahasa tersebut lebih dari satu byte ditugaskan untuk setiap huruf. Tapi apa yang kita gunakan ketika kita perlu memodifikasi satu set byte, kita menggunakan bytearray.  

Contoh

Python3




bytearray(b'\xff\x0f\xff')
0=
bytearray(b'\xff\x0f\xff')
2
bytearray(b'\xff\x0f\xff')
3
bytearray(b'\xff\x0f\xff')
4

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

bytearray(b'\xff\x0f\xff')
_6

bytearray(b'\xff\x0f\xff')
7
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
0
b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'
5=
-241
0
255
165
1
30
0
1
1

-241
0
255
165
1
30
0
1
2
-241
0
255
165
1
30
0
1
1
bytearray(b'\xff\x0f\xff')
4

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
-241
0
255
165
1
30
0
1
6

Keluaran

bytearray(b'\xff\x0f\xff')

Operasi Bitwise

Di Python, operator bitwise digunakan untuk melakukan perhitungan bitwise pada bilangan bulat. Bilangan bulat pertama-tama diubah menjadi biner dan kemudian operasi dilakukan sedikit demi sedikit, oleh karena itu disebut operator bitwise. Operasi bitwise standar ditunjukkan di bawah ini.  

Catatan. Untuk informasi lebih lanjut, lihat Operator Bitwise Python

Contoh

Python3




-241
0
255
165
1
30
0
1
7

-241
0
255
165
1
30
0
1
8

-241
0
255
165
1
30
0
1
9=
Files do not match.
1
Files do not match.
2
Files do not match.
3
Files do not match.
4
Files do not match.
5
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
2
Files do not match.
7

Files do not match.
8=
Files do not match.
1
Files do not match.
2
JPEG detected.
2
Files do not match.
4
Files do not match.
5
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
2
JPEG detected.
6

________62______7=

Files do not match.
1
Files do not match.
2bytestr1
Files do not match.
4
Files do not match.
5
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
2bytestr5

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

bytestr_7

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_6bytestr9

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

=_1

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_6=3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

=_5

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_6=7

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

=_9

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
01

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_03

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_04

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
06
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
07
bytearray(b'\xff\x0f\xff')
4

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_10

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_11

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
13
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
14
bytearray(b'\xff\x0f\xff')
4

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_17

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
18=
Files do not match.
1
Files do not match.
2
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
22
Files do not match.
4
Files do not match.
5
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
2
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
26

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_28

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_6
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
30

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_3

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_32

b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
6
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
34

Keluaran

-241
0
255
165
1
30
0
1

Beberapa Aplikasi Lainnya

Data biner menyediakan beberapa aplikasi seperti kita dapat memeriksa apakah kedua file tersebut mirip atau tidak menggunakan data biner, kita juga dapat memeriksa apakah suatu file jpeg atau tidak (atau format gambar lainnya). Mari kita lihat contoh di bawah ini untuk pemahaman yang lebih baik

Contoh 1. Memeriksa apakah kedua file tersebut sama atau tidak. Di sini dua file teks digunakan dengan data sebagai berikut –

Berkas 1

Python bekerja dengan bilangan biner

File 2

Python bekerja dengan bilangan biner

Python3




b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_35
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
36
Files do not match.
2
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
38
Files do not match.
4
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
40
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
41
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
36
Files do not match.
2
b'abc'
97
Traceback (most recent call last):
  File "bytesExample.py", line 4, in 
    bytestr[0] = 97
TypeError: 'bytes' object does not support item assignment
_44
Files do not match.
4_____4____1

Bagaimana Anda menggunakan bilangan biner dengan Python?

Dengan Python, Anda cukup menggunakan fungsi bin() untuk mengonversi dari nilai desimal ke nilai biner yang sesuai . Demikian pula, fungsi int() untuk mengonversi biner menjadi nilai desimalnya. Fungsi int() mengambil sebagai argumen kedua basis angka yang akan dikonversi, yaitu 2 dalam kasus bilangan biner.

Bagaimana cara membaca data biner dengan Python?

Fungsi open() membuka file dalam format teks secara default. Untuk membuka file dalam format biner, tambahkan 'b' ke parameter mode . Oleh karena itu mode "rb" membuka file dalam format biner untuk dibaca, sedangkan mode "wb" membuka file dalam format biner untuk ditulis. Tidak seperti file teks, file biner tidak dapat dibaca manusia.

Bagaimana Anda mendeklarasikan data biner dengan Python?

Untuk menetapkan nilai dalam format biner ke variabel, kita menggunakan akhiran 0b . Ini memberi tahu kompiler bahwa nilai (diakhiri dengan 0b) adalah nilai biner dan menugaskannya ke variabel. Catatan. Untuk mencetak nilai dalam format biner, kita menggunakan fungsi bin().

Apa itu biner dalam Python?

Pengantar bilangan Biner dengan Python. Python menyediakan sistem bilangan biner kepada pengguna, di mana kita mengubah bilangan biner menjadi bilangan desimal, sebaliknya, dan sistem bilangan biner ke oktal, yang berarti sesuai kebutuhan kita, kita dapat mengubah bilangan biner menjadi sistem bilangan lainnya