Hướng dẫn how to use left shift operator in python - cách sử dụng toán tử dịch trái trong python

Toán tử dịch chuyển bên trái Bitwise 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] 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]


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


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]
3
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]
4
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]
5
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]
6
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]
7
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]
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, 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
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ã:Đầu ra là:

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

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 như một nhận xét trong cột bên phải.

Nhà điều hành

Làm thế nào để bạn thực hiện một ca bên trái trong Python?

Toán tử dịch chuyển bên trái Bitwise x và is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.

Bài Viết Liên Quan

Chủ Đề