Hướng dẫn unsigned right shift python - python dịch chuyển phải không dấu
Stack Overflow cho các đội đang chuyển sang miền riêng của nó! Khi việc di chuyển hoàn tất, bạn sẽ truy cập các nhóm của mình tại StackoverFlowTeams.com và họ sẽ không còn xuất hiện ở thanh bên trái trên stackoverflow.com.stackoverflowteams.com, and they will no longer appear in the left sidebar on stackoverflow.com. Show Kiểm tra email của bạn để cập nhật. Toán tử dịch chuyển bên phải Python bitwise x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 80 ở bên trái và loại bỏ bit bên phải nhất. Ví dụ: nếu bạn thay đổi quyền đại diện nhị phân x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 81 theo một vị trí, bạn sẽ có được x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 82. Về mặt ngữ nghĩa, toán tử dịch chuyển bên phải bitwise giống như thực hiện phân chia số nguyên theo x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 83.bitwise right-shift operator x >> n shifts the binary representation of integer x by n positions to the right. It inserts a x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 80 bit on the left and removes the right-most bit. For example, if you right-shift the binary representation x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 81 by one position, you’d obtain x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 82. Semantically, the bitwise right-shift operator is the same as performing integer division by x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 83. Ở đây, một ví dụ tối thiểu: print(8 >> 1) # 4 print(8 >> 2) # 2 print(-3 >> 1) # -2 Hãy để lặn sâu hơn vào các chi tiết tiếp theo! Người giải thích videoKhi 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 phải [được giải thích đơn giản]] Thí dụOperands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 00bitwise right-shift operator to integer 32 shifting it by one position: x = 32 # Shift by one position to the right res = x >> 1 print(res) # 16 # Shift by two positions to the right res = x >> 2 print(res) # 8 Không có giá trị
| Bitwise hoặcThực hiện logic hoặc hoạt động trên cơ sở từng bitcomplementary binaries to represent negative integers. The first bit of a complementary binary is the sign (0: positive, 1: negative). All remaining bits encode the number. You write a negative number print(-1 >> 1) # -1 print(-2 >> 1) # -1 print(-4 >> 1) # -2 print(-4 >> 2) # -11 as the bit pattern for print(-1 >> 1) # -1 print(-2 >> 1) # -1 print(-4 >> 1) # -2 print(-4 >> 2) # -12 and flip all bits from 1 to 0 and from 0 to 1 (complement). Dưới đây là hai ví dụ đơn giản:
Hãy để sử dụng kiến thức này trong một vài ví dụ để giới thiệu hoạt động của toán tử XOR Bitwise trên các số nguyên âm: Python bitwise thay đổi bên phải trên các số nguyên âmỞ đây, kết quả của toán tử dịch chuyển bên phải bitwise
Bạn có thể thấy những ví dụ đó trong tập lệnh sau: print(-1 >> 1) # -1 print(-2 >> 1) # -1 print(-4 >> 1) # -2 print(-4 >> 2) # -1 Làm thế nào để giải quyết giá trịError: Số lượng dịch chuyển tiêu cực?Bạn không thể sử dụng số lượng dịch chuyển âm, tức là, trong biểu thức >>> 2 << 1 40 thay vì sử dụng số lượng dịch chuyển âm. Ở đây, một ví dụ về >>> 2 << 1 41: >>> 2 >> -1 Traceback (most recent call last): File " Và ở đây, một ví dụ về cách giải quyết nó bằng cách sử dụng dịch chuyển bên phải thay vì hoạt động thay đổi bên trái: >>> 2 << 1 4 Để kích hoạt toán tử dịch chuyển bên phả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 phải, phương pháp ma thuật là phương thức >>> 2 << 1 42. 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 right-shift operator, the magic method is the >>> 2 << 1 42 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:
Ở đâ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 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. 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 bitwiseCá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 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 cho số nguyên thập phân 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 và 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 cho số nguyên thập phân 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 như một nhận xét trong cột bên phải.
Trong khi làm việc như một nhà nghiên cứu trong các hệ thống phân tán, Tiến sĩ Christian Mayer đã tìm thấy tình yêu của mình đối với việc dạy các sinh viên khoa học máy tính. Để giúp học sinh đạt được thành công cao hơn của Python, ông đã thành lập trang web giáo dục chương trình Finxter.com. Ông là tác giả của cuốn sách lập trình phổ biến Python Oneer (Nostarch 2020), đồng tác giả của loạt sách Break Break Python, những cuốn sách tự xuất bản, người đam mê khoa học máy tính, freelancer và chủ sở hữu của một trong 10 blog Python lớn nhất trên toàn thế giới. Niềm đam mê của ông là viết, đọc và mã hóa. Nhưng niềm đam mê lớn nhất của anh là phục vụ các lập trình viên đầy tham vọng thông qua Finxter và giúp họ tăng cường các kỹ năng của họ. Bạn có thể tham gia học viện email miễn phí của anh ấy ở đây. |