Toán tử NOT theo chiều bit của Python ~x
đảo ngược từng bit từ biểu diễn nhị phân của số nguyên x
sao cho 0 trở thành 1 và 1 trở thành 0. Điều này về mặt ngữ nghĩa giống như tính toán
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2570. Ví dụ: biểu thức NOT theo chiều bit
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2571 trở thành
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2572,
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2573 trở thành
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2574 và
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2575 trở thành
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2576
Khi bạn xem qua bài viết, bạn có thể xem video giải thích của tôi tại đây
Python Bitwise KHÔNG từ đầu
Xem video này trên YouTube
Trong ví dụ này, bạn áp dụng toán tử NOT theo bit cho số nguyên 32
>>> ~32 -33
Biểu thức
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2575 hoạt động trên biểu diễn bit
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2578 [thập phân 32] và thực hiện bit KHÔNG dẫn đến nhị phân
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2579. Điều này tương ứng với số thập phân âm -33
x
0100000______83_______1011111Làm cách nào để bạn chuyển đổi lại số nhị phân “1011111” thành số thập phân?
- Lật từng bit trở lại
>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 255
2 - Lấy số thập phân tương ứng
>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 255
3 - Tăng nó lên một thành
>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 255
4 - Tiền tố nó với ký hiệu phủ định
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -257
6
Để hiểu phương pháp nghịch đảo này từ số nhị phân âm sang số nguyên, trước tiên bạn cần tìm hiểu một số nền tảng. Nhưng đừng lo, chỉ mất vài phút thôi. ?
Biểu diễn số nguyên âm trong nhị phân
Python sử dụng cái gọi là nhị phân bổ sung để biểu diễn các số nguyên âm. Bit đầu tiên của nhị phân bù là dấu [0. tích cực, 1. tiêu cực]. Tất cả các bit còn lại mã hóa số. Bạn viết một số âm
>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 2556 làm mẫu bit cho
>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 2557 và lật tất cả các bit từ 1 thành 0 và từ 0 thành 1 [phần bù]
Đây là hai ví dụ đơn giản
- Để đại diện cho
>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 255
8 sử dụng 8 bit, trước tiên bạn tính toán>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 255
9 rồi lật tất cả các bit để tính toánclass Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
0 - Để đại diện cho
class Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
1 sử dụng 8 bit, trước tiên bạn tính toánclass Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
2 làclass Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
3 ở đị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]class Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
4
? . Bạn có thể yêu cầu. số nguyên đã ký là gì?
- Một số nguyên có dấu, chẳng hạn sử dụng 32 bit, mã hóa một số nguyên trong phạm vi
class Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
5 - Một số nguyên không dấu mã hóa một số nguyên dương trong phạm vi
class Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
6. Số nguyên có dấu được biểu diễn bằng ký hiệu bù hai
Ví dụ toán tử Bitwise NOT của Python
Đây là kết quả của toán tử NOT bitwise ~x
khi được áp dụng cho một vài toán hạng số nguyên ví dụ x
class Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -49
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]0
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]1
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2572
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]3
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]4
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]5
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]6
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]7
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]8
class Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]9
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'0
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'1
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'2
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'3
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2574
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'5
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'6
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'7
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'8
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'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] 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]0
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]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] 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
Bạn có thể xem những ví dụ đó trong tập lệnh Python sau
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -257
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ử NOT theo chiều bit trên các số nguyên âm
Python Bitwise KHÔNG Ví dụ về số nguyên âm
Đây là kết quả của toán tử NOT theo bit ~x
khi được áp dụng cho toán hạng số nguyên âm x
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]5
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2572
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -2572
class Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -49
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]9
Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 00
Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 01
Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 02
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
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
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
Bạn có thể xem những ví dụ đó trong đoạn script sau
>>> ~-0 -1 >>> ~-1 0 >>> ~-3 2 >>> ~-9 8 >>> ~-11 10 >>> ~-256 255
Python Bitwise KHÔNG quá tải
Bạn có thể định nghĩa toán tử NOT theo chiều bit của riêng mình trên một lớp tùy chỉnh bằng cách nạp chồng phương thức
Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 07 [phương thức dunder, phương thức ma thuật] với một tham chiếu đế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: 08 làm đối số. Điều này cho phép biểu thức
~x
trên các đối tượng tùy chỉnh của bạn mà không gây ra lỗiĐây là một ví dụ
class Data: def __init__[self, data]: self.data = data def __invert__[self]: return Data[~self.data] x = Data[3] res = ~x print[res.data] # -4
Ghi chú. nếu bạn quên ghi đè lên phương thức
Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 07 mà vẫn cố sử dụng biểu thức
~x
, Python sẽ tăng một giá trị ~x
2. Bạn có thể sửa nó bằng cách định nghĩa phương thức dunder ~x
3 trong định nghĩa lớp của bạnclass Data: def __init__[self, data]: self.data = data x = Data[3] res = ~x print[res.data]
đầu ra
Traceback [most recent call last]: File "C:\Users\xcent\Desktop\code.py", line 8, in res = ~x TypeError: bad operand type for unary ~: 'Data'
Để khắc phục điều này ~x
4, chỉ cần xác định phương thức
Operands: x = 2 y = 3 Bitwise AND: 2 Bitwise OR: 3 Bitwise XOR: 1 Bitwise NOT: -3 Bitwise LEFT-SHIFT: 16 Bitwise RIGHT-SHIFT: 07 như trong ví dụ hoạt động trước đó
toán tử Bitwise
thực hiện các thao tác trên biểu diễn nhị phân [bit] của số nguyên. Bảng sau đây cung cấp tổng quan ngắn gọ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
6 cho số nguyên thập phân ~x
7 và ~x
8 cho số nguyên thập phân ~x
9 dưới dạng nhận xét ở cột bên phải
x
1. Bitwise ORThực hiện phép toán OR logic trên cơ sở từng bitx
2~Bitwise NOTThực hiện logic NOT trên cơ sở từng bit, đảo ngược từng bit sao cho 0 trở thành 1 và 1 trở thành 0. Tương tự như x
3. x
4^Bitwise XORThực hiện thao tác logic “độc quyền hoặc” trên cơ sở từng bitx
5>>Dịch chuyển phải theo từng bit Dịch chuyển nhị phân của toán hạng bên trái sang bên phải theo số lượng vị trí được chỉ định trong toán hạng bên phảix
6> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-25700>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25701
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25702
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25703
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25704
~x
3>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25706
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25707
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25708
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25709
Đây là một ví dụ về cách thực hiện các toán tử bitwise này trên một lớp tùy chỉnh
>>> ~0 -1 >>> ~1 -2 >>> ~3 -4 >>> ~9 -10 >>> ~11 -12 >>> ~256 -25710. 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] 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]
đầ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
Chris
Trong khi làm việc với tư cách là một nhà nghiên cứu trong các hệ thống phân tán, Dr. Christian Mayer tìm thấy tình yêu của mình với việc dạy sinh viên khoa học máy tính
Để giúp sinh viên đạt được mức độ thành công Python cao hơn, anh ấy đã thành lập trang web giáo dục lập trình Finxter. com. Ông là tác giả của cuốn sách lập trình nổi tiếng Python One-Liners [NoStarch 2020], đồng tác giả của loạt sách tự xuất bản Coffee Break Python, người đam mê khoa học máy tính, cộng tác viên tự do và chủ sở hữu của một trong 10 blog Python lớn nhất thế giới
Niềm đam mê của anh ấy là viết, đọc và mã hóa. Nhưng niềm đam mê lớn nhất của anh ấy là phục vụ các lập trình viên đầy tham vọng thông qua Finxter và giúp họ nâng cao kỹ năng của mình. Bạn có thể tham gia học viện email miễn phí của anh ấy tại đây