Dịch chuyển bên phải toán tử 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 làm thay đổi biểu thức nhị phân của số nguyê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 bằng các vị trí
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 sang bên phải. Nó chèn bit
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 ____01 theo một vị trí, bạn sẽ có ______02. 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 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. toán tử dịch phải 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 dịch chuyển biểu diễn nhị phân của số nguyê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 vị trí
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 sang phải. Nó chèn bit 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 vào bên trái và loại bỏ bit ngoài cùng bên phải. Ví dụ: nếu bạn dịch chuyển sang phải biểu 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ẽ nhận đượ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 phải theo bit giống như thực hiện phép chia số nguyên cho
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
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] # 84
Hãy đi 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 thanh toán dịch chuyển phải [được giải thích đơn giản]]
Thí dụ
________ 150 toán tử dịch chuyển phải theo bit thành số nguyên 32 dịch chuyển nó theo một vị trí.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
No value
Bitwise and00100000 Thực hiện hợp lý và trên cơ sở hơi00010000x = 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] # 85100001000
|
Bitwise hoặc
Thực hiện logic hoặc hoạt động hiện tại trên cơ sở từng nhị phân bit bổ sung để biểu diễn số nguyên âm. Bit đầu tiên của nhị phân bù là dấu [0. tích cực, 1. phủ định]. Tất cả các bit còn lại mã hóa số. Bạn viết số âm
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] # 821 làm mẫu bit cho
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] # 822 và lật tất cả các bit từ 1 thành 0 và từ 0 thành 1 [bổ sung]
Dưới đây là hai ví dụ đơn giản
- Để biểu thị
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
23 bằng cách sử dụng 8 bit, trước tiên bạn tính toán_______024 và sau đó là tất cả các bit để tính_______025 - Để biểu thị
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
26 bằng cách sử dụng 8 bit, bạn lần đầu tiên tính toánx = 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
27 là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
28 ở định dạng nhị phân. Sau đó, bạn bổ sung tất cả các bit để xác định phân âm nhị phân [bổ sung]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
29
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 change right on the integer integer
Ở đây, kết quả của các giao dịch toán tử bên phải 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 khi được áp dụng để ví dụ các hạng toán học số nguyên âm
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 và
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
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] # 88 [int] [int]
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] # 824 [int]
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] # 88 [nhị]
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] # 826 [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] # 826 [int]-111111111111111111-1-211111111011111111-1-4111111100111111110-2-421111110011111111
Bạn có thể thấy những ví dụ đó trong tập lệnh sau
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
Làm thế nào để giải quyết lỗi giá trị. 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
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, toán hạng thứ hai không phải âm thanh. It could not equal. Nếu bạn sử dụng số lượng dịch chuyển tiêu cực, Python sẽ tăng số lượng ____ ____39. Để giải quyết nó, hãy sử dụng thao tác thay đổi bên trái
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] # 870 thay vì sử dụng số lượng dịch chuyển âm
Ở đây, một ví dụ về
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] # 871.
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à ở đâ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
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] # 87
Để kích hoạt dịch chuyển toán tử bên phải trên các đố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á trình 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 nghịch 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, ma thuật phương pháp là phương thứ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] # 872. 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. chức năng nạp chồng toán tử. Quá tải hoạt động thông qua cái được gọi là phương thức ma thuật hoặc phương thức dunder [đối với “phương thức gạch dưới kép”]. Đối với toán tử dịch chuyển phải, phương thức kỳ diệu là phương thức ____072. 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 về các toán tử bitwise Phương pháp ma thuật
Toán tử bitwisePhương pháp ma thuật "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] # 873
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] # 874
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] # 875
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] # 876
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] # 877
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] # 878
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] # 879
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] # 800
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] # 801
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] # 802
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] # 803
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] # 872
Ở đây, một ví dụ về cách thực hiện các thuật toán bitwise này trên lớp tùy chỉnh
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] # 805. Chúng tôi đã đánh dấu toán tử tương ứng này trong mã.
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
Header is
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] # 85
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 nguyên số. 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
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] # 806 cho số nguyên thập 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] # 807 và
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] # 808 cho số nguyên thập 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] # 809 như một nhận xét trong cột bên phải. Nhà điều hành Tên Sự kiện Mô tả Thí dụ
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] # 850Không có giá trị Bitwise và Thực hiện hợp lý và trên cơ sở một chút
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] # 851. Bitwise hoặc Logic hiện thực hoặc hoạt động trên cơ sở từng bit
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] # 852~Bit whole không logic hiện thực không phải trên từng bit cơ sở, đảo ngược từng bit để 0 trở thành 1 và 1 trở thành 0. giống _
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] # 853.
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] # 854^BitWise XOR Thực hiện hoạt động độc quyền hoặc logic trực tuyến trên cơ sở từng chút một
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] # 855>>BitWise phải thay đổi Dịch chuyển nhị phân của toán hạng bên trái sang phải theo số lượng vị trí được chỉ định trong toán hạng bên phải
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] # 856