Chuỗi dịch chuyển phải trong Python

Toán tử dịch trái theo bit của Python x 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] 8

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

Đâ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

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

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

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 cho số nguyên thập phâ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
5 và
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 cho số nguyên thập phâ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
7 dưới dạng nhận xét ở cột bên phải

Toán tử TênMô tảVí dụ_______21_______8&Bitwise ANDThực hiện logic AND 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
9. Bitwise ORThực hiện phép toán OR logic trên cơ sở từng bitx

Chủ Đề