Hướng dẫn convert to reverse binary python - chuyển đổi thành python nhị phân đảo ngược

Bạn cũng có thể sử dụng bảng tra cứu [có thể được tạo khi sử dụng các phương thức ở trên]:

LUT = [0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
       8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120,
       248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180,
       116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60,
       188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50,
       178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218,
       58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214,
       54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94,
       222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81,
       209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89,
       217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85,
       213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157,
       93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147,
       83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27,
       155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23,
       151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239,
       31, 159, 95, 223, 63, 191, 127, 255]

def reverseBitOrder[uint8]:
    return LUT[uint8]

Cải thiện bài viết

Lưu bài viết

Cho một số nguyên dương và kích thước của bit, đảo ngược tất cả các bit của nó và trả về số với các bit được đảo ngược.

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  

Chúng ta có thể giải quyết vấn đề này một cách nhanh chóng trong Python. Cách tiếp cận rất đơn giản,

  1. Chuyển đổi số nguyên thành biểu diễn nhị phân của nó bằng hàm bin [num].
  2. Chức năng bin [] nối thêm 0b như một tiền tố trong biểu diễn nhị phân của số, bỏ qua hai ký tự đầu tiên của biểu diễn nhị phân và phần còn lại của chuỗi. function appends 0b as a prefix in binary representation of number, skip first two characters of binary representation and reverse remaining part of string.
  3. Như chúng ta đã biết trong bộ nhớ, bất kỳ biểu diễn nhị phân nào của một số đều được lấp đầy bằng số 0 hàng đầu sau khi đặt bit cuối cùng từ bên trái có nghĩa là chúng ta cần nối thêm số 0 [đảo ngược] số 0 sau khi đảo ngược chuỗi còn lại.bitSize – len[reversedBits] number of zeros after reversing remaining string.
  4. Bây giờ chuyển đổi biểu diễn nhị phân thành số nguyên bằng phương thức int [chuỗi, cơ sở].int[string,base] method.

Phương pháp int [] hoạt động như thế nào?

Phương thức int [chuỗi, cơ sở] lấy một chuỗi và cơ sở để xác định rằng chuỗi đang đề cập đến hệ thống số nào [nhị phân = 2, hexadecimal = 16, octal = 8, v.v.] và chuyển đổi chuỗi thành hệ thống số thập phân cho phép. Ví dụ ; int [‘1010, 2] = 10. method takes a string and base to identify that string is referring to what number system [ binary=2, hexadecimal=16, octal=8 etc. ] and converts string into decimal number system accordingly. For example ; int[‘1010’,2] = 10.

Python3

def reverseBits[num,bitSize]:

    binary = bin[num]

    

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
0=
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
2
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
3
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
4
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
5
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
4
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
5
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
3
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
4def0

    

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
0= ____1010____25

    reverseBits[num,bitSize]:3 reverseBits[num,bitSize]:4reverseBits[num,bitSize]:5reverseBits[num,bitSize]:6reverseBits[num,bitSize]:7reverseBits[num,bitSize]:8

reverseBits[num,bitSize]:9     0==     3

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
5

        6=

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
4

    binary 0__ binary 2

    binary 4

Một cách khác để hoàn nguyên các bit mà không chuyển đổi thành chuỗi:

Điều này dựa trên khái niệm rằng nếu số [nói n] được đảo ngược cho x bit thì số đảo ngược sẽ có giá trị giống như: số lượng tối đa có thể xảy ra
the maximum possible number of X bits – N
= 2X – 1 – N

Thực hiện theo các bước để thực hiện ý tưởng này:

  • Tìm số cao nhất có thể được hình thành bởi số đã cho.
  • Trừ số đã cho từ đó.
  • Trả lại các số.

Dưới đây là việc thực hiện phương pháp nhất định.

Python3

def binary 6

    binary 8= reverseBits[num,bitSize]:4

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
4 =2
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
3
Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
4

    =6 binary 8

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
3 =9

reverseBits[num,bitSize]:9     0== bin4

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  
5

        6= bin9

    [num]1= binary 2

    reverseBits[num,bitSize]:3[num]6


Bài Viết Liên Quan

Chủ Đề