Bilangan Biner Show Operator Biner, Byte, dan Bitwise dengan Python Tandai sebagai Selesai Bahan PendukungSlide 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 PengkodeanSekarang 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 ByteDari 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
b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment0 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment1 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment2 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 assignment6 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment7 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment6 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 Python3b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment6 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 assignment6 b'\xC2\xA9\x20\xF0\x9D\x8C\x86\x20\xE2\x98\x83'9 © ð â0 © ð â1 © ð â8 © ð â3 Keluaran 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 Python3bytearray(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 11 -241 0 255 165 1 30 0 12 -241 0 255 165 1 30 0 11 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 assignment6 -241 0 255 165 1 30 0 16 Keluaran bytearray(b'\xff\x0f\xff') Operasi BitwiseDi 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 17 -241 0 255 165 1 30 0 18 -241 0 255 165 1 30 0 19 = 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 assignment2 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 assignment2 JPEG detected.6 ________62______7 Files do not match.1 Files do not match.2 bytestr 1Files 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 assignment2 bytestr 5b'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_6 bytestr 9b'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_6 = 3b'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_6 = 7b'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 assignment6 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment01 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 assignment6 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment06 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment07 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 assignment6 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment13 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment14 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 assignment18 = 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 assignment22 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 assignment2 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment26 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 assignment30 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 assignment6 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment34 Keluaran -241 0 255 165 1 30 0 1 Beberapa Aplikasi LainnyaData 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 File 2 Python3b'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 assignment36 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 assignment38 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 assignment40 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment41 b'abc' 97 Traceback (most recent call last): File "bytesExample.py", line 4, in bytestr[0] = 97 TypeError: 'bytes' object does not support item assignment36 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 |