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.

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

Bài Viết Liên Quan

Chủ Đề