Toán tử dịch chuyển bên phải Python 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 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
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 video
- Thí dụ
- Đại diện cho số nguyên âm trong các nhị phân
- Python bitwise thay đổi bên phải trên các số nguyên âm
- Làm thế nào để giải quyết giá trịError: Số lượng dịch chuyển tiêu cực?
- Python BitWise Người vận hành chuyển đổi bên phải
- Các nhà khai thác bitwise
Người giải thích video
Thí dụ
Đại diện cho số nguyên âm trong các nhị phân
Thí dụ
Đại diện cho số nguyên âm trong các nhị phânbitwise 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
Python bitwise thay đổi bên phải trên các số nguyên âm
Làm thế nào để giải quyết giá trịError: Số lượng dịch chuyển tiêu cực? | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Python BitWise Người vận hành chuyển đổi bên phải | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
Các nhà khai thác bitwise | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
Khi bạn xem bài viết, bạn có thể xem video giải thích của tôi ở đây:
Đại diện cho số nguyên âm trong các nhị phân
Python bitwise thay đổi bên phải trên các số nguyên âmcomplementary 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].
Làm thế nào để giải quyết giá trịError: Số lượng dịch chuyển tiêu cực?
- Python BitWise Người vận hành chuyển đổi bên phải
- Các nhà khai thác bitwise
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 thay đổi bên phải trên các số nguyên âm
Làm thế nào để giải quyết giá trịError: Số lượng dịch chuyển tiêu cực?
-1 | 1 | 11111111 | 11111111 | -1 |
-2 | 1 | 11111110 | 11111111 | -1 |
-4 | 1 | 11111100 | 11111110 | -2 |
-4 | 2 | 11111100 | 11111111 | -1 |
Đại diện bit của 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] # 84 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] # 85. Nếu bạn chuyển nó theo một vị trí sang bên phải, bạn sẽ có được 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] # 86 "[thập phân 16]. 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 right res = x >> 1 print[res] # 16 # Shift by two positions to the right res = x >> 2 print[res] # 87" [thập phân 8]. Ở đây, lời giải thích bảng:
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?
Python BitWise Người vận hành chuyển đổi bên phải
Các nhà khai thác bitwise
>>> 2 >> -1 Traceback [most recent call last]: File "", line 1, in 2 >> -1 ValueError: negative shift count
Khi bạn xem bài viết, bạn có thể xem video giải thích của tôi ở đây:
>>> 2 >> 2 > 1 print[res] # 16 # Shift by two positions to the right res = x >> 2 print[res] # 84 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] # 85. Nếu bạn chuyển nó theo một vị trí sang bên phải, bạn sẽ có được 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] # 86 "[thập phân 16]. 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 right res = x >> 1 print[res] # 16 # Shift by two positions to the right res = x >> 2 print[res] # 87" [thập phân 8]. Ở đây, lời giải thích bả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] # 88
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] # 89
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -10
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -11 làm mẫu bit cho
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -12 và lật tất cả các bit từ 1 đến 0 và từ 0 đến 1 [bổ sung].
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -13 bằng cách sử dụng 8 bit, trước tiên bạn tính toán
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -14 và sau đó lật tất cả các bit để tính
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -15.
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -16 bằng cách sử dụng 8 bit bạn đầu tiên tính toán
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -17 là
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -18 ở định dạng nhị phân. Sau đó, bạn bổ sung tất cả các bit để xác định nhị phân âm [bổ sung]
print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -19.
x >> n
khi được áp dụng để ví dụ các toán hạng số nguyên âm x
và 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] # 88 [int]
>>> 2 >> -1 Traceback [most recent call last]: File "", line 1, in 2 >> -1 ValueError: negative shift count4 [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ị phân]
>>> 2 >> -1 Traceback [most recent call last]: File "", line 1, in 2 >> -1 ValueError: negative shift count6 [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] 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]
>>> 2 >> -1 Traceback [most recent call last]: File "", line 1, in 2 >> -1 ValueError: negative shift count6 [int]
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
Khi bạn xem bài viết, bạn có thể xem video giải thích của tôi ở đây:
Đại diện bit của 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] # 84 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] # 85. Nếu bạn chuyển nó theo một vị trí sang bên phải, bạn sẽ có được 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] # 86 "[thập phân 16]. 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 right res = x >> 1 print[res] # 16 # Shift by two positions to the right res = x >> 2 print[res] # 87" [thập phân 8]. Ở đây, lời giải thích bảng: | 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] # 88 | |||
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] # 89 | print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -10 | 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 sử dụng cái gọi là nhị phân bổ sung để thể hiện số nguyên âm. Bit đầu tiên của một nhị phân bổ sung là dấu hiệu [0: dương, 1: âm]. Tất cả các bit còn lại mã hóa số. Bạn viết một số âm print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -11 làm mẫu bit cho print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -12 và lật tất cả các bit từ 1 đến 0 và từ 0 đến 1 [bổ sung]. |
Dưới đây là hai ví dụ đơn giản: | Để biểu thị print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -13 bằng cách sử dụng 8 bit, trước tiên bạn tính toán print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -14 và sau đó lật tất cả các bit để tính print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -15. | Để biểu thị print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -16 bằng cách sử dụng 8 bit bạn đầu tiên tính toán print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -17 là print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -18 ở định dạng nhị phân. Sau đó, bạn bổ sung tất cả các bit để xác định nhị phân âm [bổ sung] print[-1 >> 1] # -1 print[-2 >> 1] # -1 print[-4 >> 1] # -2 print[-4 >> 2] # -19. | 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: |
Ở đây, kết quả của toán tử dịch chuyển bên phải bitwise x >> n khi được áp dụng để ví dụ các toán hạng số nguyên âm x và 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] # 88 [int] | Thực hiện logic không phải trên cơ sở từng bit, đảo ngược mỗi bit để 0 trở thành 1 và 1 trở thành 0. giống 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: 03. | Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 04 |
^ | BitWise XOR | Thực hiện hoạt động độc quyền hoặc trực tuyến logic trên cơ sở từng chút một | Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 05 |
>> | 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 | Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 06 |
> có nghĩa là gì trong Python?
Trong Python >> được gọi là toán tử dịch chuyển đúng.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.right shift operator. It is a 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.
Chủ Đề |