Hướng dẫn what does left shift operator do in python? - toán tử dịch chuyển trái làm gì 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 what does left shift operator do in python? - toán tử dịch chuyển trái làm gì 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 what does left shift operator do in python? - toán tử dịch chuyển trái làm gì 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

Ý nghĩa của

3. Các nhà khai thác so sánh Python.

Bên trái còn lại là gì?

Shift-Heft là thực hành di chuyển thử nghiệm, chất lượng và đánh giá hiệu suất sớm trong quá trình phát triển, thường là trước khi có mã nào được viết.Kiểm tra dịch chuyển-bên trái giúp các nhóm dự đoán các thay đổi phát sinh trong quá trình phát triển có thể ảnh hưởng đến hiệu suất hoặc các quy trình giao hàng khác.the practice of moving testing, quality, and performance evaluation early in the development process, often before any code is written. Shift-left testing helps teams anticipate changes that arise during the development process that can affect performance or other delivery processes.

Những gì còn lại thay đổi sang phải?

Các toán tử dịch chuyển bitwise di chuyển các giá trị bit của một đối tượng nhị phân.Toán hạng bên trái chỉ định giá trị sẽ được thay đổi. Toán tử bên phải chỉ định số lượng vị trí mà các bit trong giá trị sẽ được thay đổi.Kết quả không phải là một lvalue.Cả hai toán hạng có cùng mức độ ưu tiên và được kết hợp từ trái sang phải.The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted. The result is not an lvalue. Both operands have the same precedence and are left-to-right associative.

Làm thế nào để Bit Shift còn lại hoạt động?

Chuyển bit logic có thể hữu ích để nhân hoặc chia số nguyên không dấu cho sức mạnh của hai.Ví dụ: nếu giá trị "0001" hoặc "1" được dịch chuyển sang trái, nó sẽ trở thành "0010" hoặc "2", chuyển sang bên trái một lần nữa, nó trở thành "0100" hoặc "4."Việc dịch chuyển sang bên phải có tác dụng ngược lại là chia giá trị cho hai ca cho mỗi ca.if the value "0001" or "1" is shifted left, it becomes "0010" or "2," shifted to the left again it becomes "0100," or "4." Shifting to the right has an opposite effect of dividing the value by two per shift.