Python đại diện cho số âm như thế nào?
Toán tử NOT theo chiều bit của Python >>> ~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 Show 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?
Để 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ânPython 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
? . Bạn có thể yêu cầu. số nguyên đã ký là gì?
Ví dụ toán tử Bitwise NOT của PythonĐây là kết quả của toán tử NOT bitwise 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) 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 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 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 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 >>> ~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) 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 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ảiBạ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 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ử Bitwisethự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 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<x 8x 9>>> ~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) 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 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 Python có hoạt động với số âm không?điều kiện, Python có thể in số mà người dùng nhập vào là số dương, số âm hay số 0 .
Số âm được biểu diễn dưới dạng dữ liệu như thế nào?Các số âm có thể được biểu diễn theo hai cách. dấu và độ lớn . bù hai.
Làm thế nào các giá trị âm được lưu trữ trong Python?Bất cứ khi nào truy cập vào giá trị đó trước tiên, bit dấu sẽ được kiểm tra nếu bit dấu là 1 thì nhị phân sẽ được bù hai và được chuyển đổi thành số thập phân tương đương và sẽ là . Phần bù 2 của nhị phân trên là. 11111111111111111111011111111000. . 2's complement of the above binary is: 11111111111111111111011111111000. |