Daftar operator shift dengan python

Operator geser kiri bitwise Python x << n menggeser representasi biner dari bilangan bulat x dengan n posisi ke kiri. Untuk bilangan bulat positif, ia menyisipkan bit 0 di sebelah kanan dan menggeser semua bit yang tersisa dengan satu posisi ke kiri. Misalnya, jika Anda menggeser representasi biner 0101 ke kiri dengan satu posisi, Anda akan mendapatkan 01010. Secara semantik, operator geser kiri bitwise x << n sama dengan mengalikan bilangan bulat x dengan

x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128
2

Daftar operator shift dengan python

Ini contoh minimal

print(8 << 1)
# 16

print(8 << 2)
# 32

print(-3 << 1)
# -6
_

Mari selami detailnya lebih dalam selanjutnya

Video Penjelasan

Saat Anda membaca artikel, Anda dapat menonton video penjelasan saya di sini

Operator Geser Kiri Python Bitwise

Daftar operator shift dengan python

Tonton video ini di YouTube

Contoh

Dalam contoh ini, Anda menerapkan operator geser kiri bitwise ke bilangan bulat 32 dengan menggesernya satu posisi

x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128

Representasi bit desimal

x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128
3 adalah
x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128
4. Jika Anda menggesernya satu posisi ke kiri, Anda mendapatkan biner
x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128
5" (desimal 64). Jika Anda menggeser dua posisi ke kanan, Anda mendapatkan biner
x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128
6" (desimal 128). Berikut penjelasan tabelnya

x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128
70100000________0______801000000
x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128
9010000000

Setiap baris mewakili representasi biner bergeser yang dihasilkan dari bilangan bulat asli 32

Overload Operator Geser Kiri Bitwise Python

Untuk mengaktifkan operator shift kiri pada objek khusus Anda, gunakan fungsionalitas kelebihan beban operator Python. Overloading bekerja melalui apa yang disebut metode ajaib atau metode dunder (untuk “metode garis bawah ganda”). Untuk operator shift kiri, metode ajaibnya adalah metode

class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
0. Itu harus mengembalikan objek khusus baru yang merupakan hasil dari operasi bitwise

Berikut ini ikhtisar singkat tentang metode ajaib operator Bitwise

Bitwise OperatorMagic “Dunder” Method
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
1
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
2
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
3
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
4
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
5
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
6
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
7
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
8
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
9
class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)
0
Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
1
Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
2

Berikut adalah contoh cara menyelesaikan operator bitwise ini pada kelas khusus

Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
3. Kami menandai masing-masing operator ini dalam kode

class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)

Keluarannya adalah

Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0

Operator Bitwise

melakukan operasi pada representasi biner (bit) dari bilangan bulat. Tabel berikut memberikan ikhtisar singkat tentang semua operator bitwise yang ada. Perhatikan bahwa kami juga menyediakan representasi biner

Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
4 untuk bilangan bulat desimal
Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
5, dan
Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
6 untuk bilangan bulat desimal
Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
7 sebagai komentar di kolom kanan

OperatorNameDeskripsiContoh
Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
8&Bitwise ANDMelakukan logika DAN sedikit demi sedikit
Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0
9. Bitwise ORMelakukan operasi logis ATAU pada basis bit-demi-bitx << n0~Bitwise NOTMelakukan logika NOT pada basis bit-demi-bit, membalikkan setiap bit sehingga 0 menjadi 1 dan 1 menjadi 0. Sama seperti x << n1. x << n2^Bitwise XORMelakukan operasi logis "eksklusif atau" pada basis bit-demi-bitx << n3>>Pergeseran bitwise kanan Menggeser biner operan kiri ke kanan dengan jumlah posisi yang ditentukan dalam operan kananx << n4<

Daftar operator shift dengan python

Chris

Saat bekerja sebagai peneliti dalam sistem terdistribusi, Dr. Christian Mayer menemukan cintanya untuk mengajar siswa ilmu komputer

Untuk membantu siswa mencapai tingkat kesuksesan Python yang lebih tinggi, dia mendirikan situs web pendidikan pemrograman Finxter. com. Dia adalah penulis buku pemrograman populer Python One-Liners (NoStarch 2020), rekan penulis seri Coffee Break Python dari buku yang diterbitkan sendiri, penggemar ilmu komputer, pekerja lepas, dan pemilik salah satu dari 10 blog Python terbesar di dunia

Kesukaannya adalah menulis, membaca, dan coding. Tetapi hasrat terbesarnya adalah untuk melayani calon pembuat kode melalui Finxter dan membantu mereka meningkatkan keterampilan mereka. Anda dapat bergabung dengan akademi email gratisnya di sini

Apa itu operator shift di Python?

Python bitwise operator shift kiri menggeser bit operan kiri ke sisi kiri untuk jumlah waktu tertentu dalam operan kanan . Secara sederhana, bilangan biner ditambahkan dengan 0 di bagian akhir.

Ada berapa jenis operator shift?

Ada tiga jenis operator shift di Jawa. Operator Shift Kiri Tertanda (<

Apa itu >> dan < dengan Python?