Hướng dẫn unsigned right shift python - python dịch chuyển phải không dấu

Stack Overflow cho các đội đang chuyển sang miền riêng của nó! Khi việc di chuyển hoàn tất, bạn sẽ truy cập các nhóm của mình tại StackoverFlowTeams.com và họ sẽ không còn xuất hiện ở thanh bên trái trên stackoverflow.com.stackoverflowteams.com, and they will no longer appear in the left sidebar on stackoverflow.com.

Kiểm tra email của bạn để cập nhật.

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)
# 8
0 ở 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)
# 8
1 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)
# 8
2. 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)
# 8
3.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)
# 8
0 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)
# 8
1 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)
# 8
2. 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)
# 8
3.

Hướng dẫn unsigned right shift python - python dịch chuyển phải không dấu

Ở đâ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

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 toán tử dịch chuyển phải [được giải thích đơn giản]]

Thí dụ

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
0bitwise 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

Không có giá trị

Bitwise và0 0 1 0 0 0 0 0
Thực hiện hợp lý và trên cơ sở một chút0 0 0 1 0 0 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
0 0 0 0 1 0 0 0

|

Bitwise hoặc

Thực hiện logic hoặc hoạt động trên cơ sở từng bitcomplementary 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)
# -1
1 as the bit pattern for
print(-1 >> 1)
# -1

print(-2 >> 1)
# -1

print(-4 >> 1)
# -2

print(-4 >> 2)
# -1
2 and flip all bits from 1 to 0 and from 0 to 1 (complement).

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)
    # -1
    
    3 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)
    # -1
    
    4 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)
    # -1
    
    5.
  • Để biểu thị
    print(-1 >> 1)
    # -1
    
    print(-2 >> 1)
    # -1
    
    print(-4 >> 1)
    # -2
    
    print(-4 >> 2)
    # -1
    
    6 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)
    # -1
    
    7 là
    print(-1 >> 1)
    # -1
    
    print(-2 >> 1)
    # -1
    
    print(-4 >> 1)
    # -2
    
    print(-4 >> 2)
    # -1
    
    8 ở đị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)
    # -1
    
    9.

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 thay đổi bên phải 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 xn:

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
8 (int) (int)
>>> 2 >> -1
Traceback (most recent call last):
  File "", line 1, in 
    2 >> -1
ValueError: negative shift count
4 (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)
# 8
8 (nhị phân)
>>> 2 >> -1
Traceback (most recent call last):
  File "", line 1, in 
    2 >> -1
ValueError: negative shift count
6 (nhị phân)
>>> 2 >> -1
Traceback (most recent call last):
  File "", line 1, in 
    2 >> -1
ValueError: negative shift count
6 (int)
-1 1 11111111 11111111 -1
-2 1 11111110 11111111 -1
-4 1 11111100 11111110 -2
-4 2 11111100 11111111 -1

Bạn có thể thấy những ví dụ đó trong tập lệnh sau:

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?

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 >> n, toán hạng N thứ hai phải không âm. Nó có thể bằng không. 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

>>> 2 << 1
4
0 thay vì sử dụng số lượng dịch chuyển âm.

Ở đây, một ví dụ về

>>> 2 << 1
4
1:

>>> 2 >> -1
Traceback (most recent call last):
  File "", line 1, in 
    2 >> -1
ValueError: negative shift count

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:

>>> 2 << 1
4

Để kích hoạt toán tử dịch chuyển bên phải trên đố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á 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 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, phương pháp ma thuật là phương thức

>>> 2 << 1
4
2. 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.operator overloading functionality. Overloading works through what is called magic methods or dunder methods (for “double-underscore methods”). For the right-shift operator, the magic method is the
>>> 2 << 1
4
2 method. It should return a new custom object that is the result of the bitwise operation.

Tại đây, một cái nhìn tổng quan ngắn về các toán tử bitwise Phương pháp ma thuật:

Toán tử bitwisePhương pháp ma thuật "
>>> 2 << 1
4
3
>>> 2 << 1
4
4
>>> 2 << 1
4
5
>>> 2 << 1
4
6
>>> 2 << 1
4
7
>>> 2 << 1
4
8
>>> 2 << 1
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)
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
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)
3
>>> 2 << 1
4
2

Ở đây, một ví dụ về cách thực hiện các toán tử bitwise này trên lớp tùy chỉnh

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. 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

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 số nguyên. 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

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)
6 cho số nguyên thập 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)
    
    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)
7 và
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)
8 cho số nguyên thập 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)
    
    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 như một nhận xét trong cột bên phải.

Nhà điều hànhTênSự mô tả Thí dụ
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
Không có giá trịBitwise vàThực hiện hợp lý và trên cơ sở một chú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:  0
1
|Bitwise hoặcThực hiện logic hoặc hoạt động trên cơ sở từng bit
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
~Bit whole khôngThự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:  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
^BitWise XORThự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:  0
5
>>BitWise phải thay đổiDị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:  0
6
<Bitwise trái thay đổiDịch chuyển nhị phân của toán hạng bên trái sang trái bởi 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:  0
7

Hướng dẫn unsigned right shift python - python dịch chuyển phải không dấu

Trong khi làm việc như một nhà nghiên cứu trong các hệ thống phân tán, Tiến sĩ Christian Mayer đã tìm thấy tình yêu của mình đối với việc dạy các sinh viên khoa học máy tính.

Để giúp học sinh đạt được thành công cao hơn của Python, ông đã thành lập trang web giáo dục chương trình Finxter.com. Ông là tác giả của cuốn sách lập trình phổ biến Python Oneer (Nostarch 2020), đồng tác giả của loạt sách Break Break Python, những cuốn sách tự xuất bản, người đam mê khoa học máy tính, freelancer và chủ sở hữu của một trong 10 blog Python lớn nhất trên toàn thế giới.

Niềm đam mê của ông là viết, đọc và mã hóa. Nhưng niềm đam mê lớn nhất của anh là phục vụ các lập trình viên đầy tham vọng thông qua Finxter và giúp họ tăng cường các kỹ năng của họ. Bạn có thể tham gia học viện email miễn phí của anh ấy ở đây.