Hướng dẫn how to use left shift operator in python - cách sử dụng toán tử dịch trái trong python

Toán tử dịch chuyển bên trái Bitwise x << n làm thay đổi biểu diễn nhị phân của số nguyên x bằng các vị trí n sang trái. Đối với một số nguyên dương, nó chèn bit 0 ở bên phải và chuyển tất cả các bit còn lại theo một vị trí sang trái. Ví dụ: nếu bạn thay đổi bên trái, biểu diễn nhị phân 0101 theo một vị trí, bạn sẽ có được 01010. Về mặt ngữ nghĩa, toán tử dịch chuyển bên trái bitwise x << n giống như nhân số nguyên x với

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.bitwise left-shift operator x << n shifts the binary representation of integer x by n positions to the left. For a positive integer, it inserts a 0 bit on the right and shifts all remaining bits by one position to the left. For example, if you left-shift the binary representation 0101 by one position, you’d obtain 01010. Semantically, the bitwise left-shift operator x << n is the same as multiplying the integer x with
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.

Hướng dẫn how to use left shift operator in python - cách sử dụng toán tử dịch trái trong python

Ở đây, một ví dụ tối thiểu:

print(8 << 1)
# 16

print(8 << 2)
# 32

print(-3 << 1)
# -6

Hãy để lặn sâu hơn vào các chi tiết tiếp theo!

Người giải thích video

Khi bạn xem bài viết, bạn có thể xem video giải thích của tôi ở đây:

Python bitwise toán tử dịch chuyển bên trái

Thí dụ

Trong ví dụ này, bạn áp dụng toán tử dịch chuyển bên trái bitwise vào số nguyên 32 chuyển nó theo một vị trí:bitwise left-shift operator to integer 32 shifting it by one position:

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

Đại diện bit của thập phân

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 là
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. Nếu bạn chuyển nó theo một vị trí sang trái, bạn sẽ có được nhị phân
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 "(thập phân 64). Nếu bạn chuyển theo hai vị trí sang bên phải, bạn sẽ có được nhị phân
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" (thập phân 128). Ở đây, lời giải thích bảng:

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
7
0 1 0 0 0 0 0
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
8
0 1 0 0 0 0 0 0
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
9
0 1 0 0 0 0 0 0 0

Mỗi hàng đại diện cho biểu diễn nhị phân thay đổi kết quả của số nguyên gốc 32.

Python BitWise Người vận hành thay đổi bên trái

Để kích hoạt toán tử dịch chuyển bên trái trên đối tượng tùy chỉnh của bạn, hãy sử dụng chức năng quá tải của toán tử Python. Quá tải hoạt động thông qua cái được gọi là Phương pháp ma thuật hoặc phương pháp Dunder (đối với các phương thức kép hai lần). Đối với toán tử dịch chuyển bên trái, phương pháp ma thuật là phương pháp

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. Nó sẽ trả về một đối tượng tùy chỉnh mới là kết quả của hoạt động bitwise.operator overloading functionality. Overloading works through what is called magic methods or dunder methods (for “double-underscore methods”). For the left-shift operator, the magic method is the
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 method. It should return a new custom object that is the result of the bitwise operation.

Tại đây, một cái nhìn tổng quan ngắn về các toán tử bitwise Phương pháp ma thuật:

Toán tử bitwisePhương pháp ma thuật "
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

Ở đây, một ví dụ về cách thực hiện các toán tử bitwise này trên lớp tùy chỉnh

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. Chúng tôi đã đánh dấu toán tử tương ứng này trong mã:

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)

Đầu ra là:

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

Các nhà khai thác bitwise

Các toán tử bitwise thực hiện các hoạt động trên biểu diễn nhị phân (bit) của các số nguyên. Bảng sau đây đưa ra một cái nhìn tổng quan ngắn về tất cả các toán tử bitwise hiện có. Lưu ý rằng chúng tôi cũng cung cấp biểu diễn nhị phân

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 cho số nguyên thập phân
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 và
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 cho số nguyên thập phân
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 như một nhận xét trong cột bên phải.

Nhà điều hànhTênSự mô tả Thí dụ
Trong ví dụ này, bạn áp dụng toán tử dịch chuyển bên trái bitwise vào số nguyên 32 chuyển nó theo một vị trí:
Đại diện bit của thập phân
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 là
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. Nếu bạn chuyển nó theo một vị trí sang trái, bạn sẽ có được nhị phân
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 "(thập phân 64). Nếu bạn chuyển theo hai vị trí sang bên phải, bạn sẽ có được nhị phân
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" (thập phân 128). Ở đây, lời giải thích bảng:
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
7
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
8
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
9
Mỗi hàng đại diện cho biểu diễn nhị phân thay đổi kết quả của số nguyên gốc 32.Python BitWise Người vận hành thay đổi bên tráiĐể kích hoạt toán tử dịch chuyển bên trái trên đối tượng tùy chỉnh của bạn, hãy sử dụng chức năng quá tải của toán tử Python. Quá tải hoạt động thông qua cái được gọi là Phương pháp ma thuật hoặc phương pháp Dunder (đối với các phương thức kép hai lần). Đối với toán tử dịch chuyển bên trái, phương pháp ma thuật là phương pháp
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. Nó sẽ trả về một đối tượng tùy chỉnh mới là kết quả của hoạt động bitwise.
Tại đây, một cái nhìn tổng quan ngắn về các toán tử bitwise Phương pháp ma thuật:
Toán tử bitwisePhương pháp ma thuật "
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
Ở đây, một ví dụ về cách thực hiện các toán tử bitwise này trên lớp tùy chỉnh
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. Chúng tôi đã đánh dấu toán tử tương ứng này trong mã:
Đầu ra là:

Hướng dẫn how to use left shift operator in python - cách sử dụng toán tử dịch trái trong python

Các nhà khai thác bitwise

Các toán tử bitwise thực hiện các hoạt động trên biểu diễn nhị phân (bit) của các số nguyên. Bảng sau đây đưa ra một cái nhìn tổng quan ngắn về tất cả các toán tử bitwise hiện có. Lưu ý rằng chúng tôi cũng cung cấp biểu diễn nhị phân

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 cho số nguyên thập phân
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 và
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 cho số nguyên thập phân
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 như một nhận xét trong cột bên phải.

Nhà điều hành

Làm thế nào để bạn thực hiện một ca bên trái trong Python?

Toán tử dịch chuyển bên trái Bitwise x x << n shifts the binary representation of integer x by n positions to the left. For a positive integer, it inserts a 0 bit on the right and shifts all remaining bits by one position to the left.

Điều gì được sử dụng cho trong Python?

Nó là một toán tử bitwise.Nó đòi hỏi một biểu diễn bitwise của đối tượng là toán hạng đầu tiên.Các bit được chuyển sang phải theo số bit được quy định bởi toán hạng thứ hai.Các bit dẫn đầu như phía bên trái là kết quả của việc dịch chuyển được đặt thành 0.bitwise operator. It requires a bitwise representation of object as first operand. Bits are shifted to right by number of bits stipulated by second operand. Leading bits as towards left as a result of shifting are set to 0.

Toán tử thay đổi còn lại làm gì trong Python?

Python bitwise toán tử chuyển đổi bên trái dịch chuyển các bit toán hạng bên trái về phía bên trái cho số lần đã cho trong toán hạng bên phải.Nói một cách đơn giản, số nhị phân được thêm vào với 0 ở cuối.shifts the left operand bits towards the left side for the given number of times in the right operand. In simple terms, the binary number is appended with 0s at the end.

>> và
|<< is the left shift and >> is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.