Python đại diện cho số âm như thế nào?

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
-257
0. Ví dụ: biểu thức NOT theo chiều bit
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
1 trở thành
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
2,
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
3 trở thành
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
4 và
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
5 trở thành
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
6

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

Python đại diện cho số âm như thế nào?

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
-257
5 hoạt động trên biểu diễn bit
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
8 (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
-257
9. Điều này tương ứng với số thập phân âm -33

x0100000______83_______1011111

Là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
255
6 làm mẫu bit cho
>>> ~-0
-1
>>> ~-1
0
>>> ~-3
2
>>> ~-9
8
>>> ~-11
10
>>> ~-256
255
7 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án
    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
    
    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án
    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
    
    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

x (int)x (binary)~x (binary)~x (int)
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
9
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
-257
2
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
-257
4
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 ~x khi được áp dụng cho toán hạng số nguyên âm x

x (int)~x (int)
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
-257
2
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
2
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
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)
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:  0
0
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
1
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
2
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
3
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
4
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
5
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
6

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:  0
7 (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:  0
8 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:  0
7 mà vẫn cố sử dụng biểu thức ~x, Python sẽ tăng một giá trị ~x2. Bạn có thể sửa nó bằng cách định nghĩa phương thức dunder ~x3 trong định nghĩa lớp của bạn

class 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 ~x4, 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:  0
7 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 ~x6 cho số nguyên thập phân ~x7 và ~x8 cho số nguyên thập phân ~x9 dưới dạng nhận xét ở cột bên phải

Toán tử TênMô tảVí dụ_______84_______0&Bitwise ANDThực hiện logic AND trên cơ sở từng chút mộtx1. Bitwise ORThực hiện phép toán OR logic trên cơ sở từng bitx2~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ư x3. x4^Bitwise XORThực hiện thao tác logic “độc quyền hoặc” trên cơ sở từng bitx5>>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ảix6<Dưới đây là tổng quan ngắn về các phương thức ma thuật của toán tử Bitwise

Bitwise OperatorMagic “Dunder” Methodx8x9
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
00
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
01
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
02
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
03
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
04~x3
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
06
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
07
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
08
>>> ~0
-1
>>> ~1
-2
>>> ~3
-4
>>> ~9
-10
>>> ~11
-12
>>> ~256
-257
09

Đâ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
-257
10. 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

Python đại diện cho số âm như thế nào?

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.