Toán tử dịch trái theo bit của Python Đâ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 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]
9class 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]
0Operands:
x = 2
y = 3
Bitwise AND: 2
Bitwise OR: 3
Bitwise XOR: 1
Bitwise NOT: -3
Bitwise LEFT-SHIFT: 16
Bitwise RIGHT-SHIFT: 0
1Operands:
x = 2
y = 3
Bitwise AND: 2
Bitwise OR: 3
Bitwise XOR: 1
Bitwise NOT: -3
Bitwise LEFT-SHIFT: 16
Bitwise RIGHT-SHIFT: 0
2Operands:
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: 04 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: 05 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: 06 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: 07 dưới dạng nhận xét ở cột 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: 09. Bitwise ORThực hiện phép toán OR logic trên cơ sở từng bit
x