简体   繁体   English

如何将表示为数字数组的数字从 base-2^k 转换为二进制?

[英]How would I convert a number represented as an array of digits from base-2^k to binary?

I have an algorithm that can simulate converting a binary number to a decimal number by hand.我有一个算法可以模拟手动将二进制数转换为十进制数。 What I mean by this is that each number is represented as an array of digits (from least-to-most-significant) rather than using a language's int or bigint type.我的意思是每个数字都表示为一个数字数组(从最不重要到最重要),而不是使用语言的 int 或 bigint 类型。

For example, 42 in base-10 would be represented as [2, 4], and 10111 in base-2 would be [1, 1, 1, 0, 1].例如,base-10 中的 42 将表示为 [2, 4],base-2 中的 10111 将表示为 [1, 1, 1, 0, 1]。

Here it is in Python.这是在 Python 中。

def double(decimal):
    result = []
    carry = 0
    for i in range(len(decimal)):
        result.append((2 * decimal[i] + carry) % 10)
        carry = floor((2 * decimal[i] + carry) / 10)
    if carry != 0:
        result.append(carry)
    return result

def to_decimal(binary):
    decimal = []
    for i in reversed(range(len(binary))):
        decimal = double(decimal)
        if binary[i]:
            if decimal == []:
                decimal = [1]
            else:
                decimal[0] += 1
    return decimal

This was part of an assignment I had with an algorithms class a couple of semesters ago, and he gave us a challenge in his notes claiming that we should be able to derive from this algorithm a new one that could convert a number from base-2^k to binary.这是我几个学期前在算法课上的作业的一部分,他在笔记中给我们提出了一个挑战,声称我们应该能够从这个算法中推导出一个可以从基数 2 转换数字的新算法^k 到二进制。 I dug this up today and it's been bothering me (read: making me feel really rusty), so I was hoping someone would be able to explain how I would write a to_binary(number, k) function based on this algorithm.我今天发现了这个问题,它一直困扰着我(阅读:让我感到非常生疏),所以我希望有人能够解释我将如何基于此算法编写to_binary(number, k)函数。

Base 2^k has digits 0, 1, ..., 2^k - 1 .基数2^k有数字0, 1, ..., 2^k - 1

For example, in base 2^4 = 16 , we'd have the digits 0, 1, 2, ..., 10, 11, 12, 13, 14, 15 .例如,在基数2^4 = 16 ,我们有数字0, 1, 2, ..., 10, 11, 12, 13, 14, 15 For convenience, we use letters for the bigger digits: 0, 1, ..., A, B, C, D, E, F .为方便起见,我们对较大的数字使用字母: 0, 1, ..., A, B, C, D, E, F

So let's say you want to convert AB to binary.因此,假设您想将AB转换为二进制。 The trivial thing to do is convert it to decimal first, since we know how to convert decimal to binary:最简单的事情是先将其转换为十进制,因为我们知道如何将十进制转换为二进制:

AB = B*16^0 + A*16^1 
   = 11*16^0 + 10*16^1
   = 171

If you convert 171 to binary, you'll get:如果将171转换为二进制,您将得到:

10101011

Now, is there a shortcut we can use, so we don't go through base 10?现在,有没有我们可以使用的快捷方式,所以我们不通过基数 10? There is.有。

Let's stop at this part:让我们在这部分停下来:

AB = B*16^0 + A*16^1 
   = 11*16^0 + 10*16^1

And recall what it takes to convert from decimal to binary: do integer division by 2, write down the remainders, write the remainders in reverse order in the end:回想一下从十进制转换为二进制需要什么:将整数除以 2,记下余数,最后以相反的顺序写出余数:

number after integer division by 2 | remainder after integer division by 2
--------------------------------------------------------------------------
                                 5 | 1
                                 2 | 0
                                 1 | 1
                                 0 |

                  => 5 = reverse(101) = 101 in binary

Let's apply that to this part:让我们将其应用于这一部分:

11*16^0 + 10*16^1 

First of all, for the first 4 (because 16^1 = 2^4 ) divisions, the remainder of division by 2 will only depend on 11 , because 16 % 2 == 0 .首先,对于前4 (因为16^1 = 2^4 )除法,除以2的余数将仅取决于11 ,因为16 % 2 == 0

11 | 1
5  | 1
2  | 0
1  | 1
0  |

So the last part of our number in binary will be:所以我们二进制数的最后一部分将是:

1011

By the time we've done this, we will have gotten rid of the 16^1 , since we've done 4 divisions so far.到我们完成此操作时,我们将摆脱16^1 ,因为到目前为止我们已经完成了4分区。 So now we only depend on 10 :所以现在我们只依赖10

10 | 0
 5 | 1
 2 | 0
 1 | 1
 0 |

So our final result will be:所以我们的最终结果将是:

10101011

Which is what we got with the classic approach!这就是我们用经典方法得到的!

As we can notice, we only need to convert the digits to binary individually, because they are what will, individually and sequentially, affect the result:正如我们所注意到的,我们只需要将数字单独转换为二进制,因为它们会单独和顺序地影响结果:

A = 10 = 1010
B = 11 = 1011

=> AB in binary = 10101011

For your base 2^k , do the same: convert each individual digit to binary, from most significant to least, and concatenate the results in order.对于您的基数2^k ,执行相同的操作:将每个单独的数字转换为二进制,从最重要到最不重要,并按顺序连接结果。

Example implementation:示例实现:

def to_binary(number, k):
    result = []
    for x in number:
        # convert x to binary
        binary_x = []
        t = x
        while t != 0:
            binary_x.append(t % 2)
            t //= 2
        result.extend(binary_x[::-1])

    return result

#10 and 11 are digits here, so this is like AB.
print(to_binary([10, 11], 2**4))
print(to_binary([101, 51, 89], 2**7))

Prints:印刷:

[1, 0, 1, 0, 1, 0, 1, 1]
[1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1]

Note: there is actually a bug in the above code.注意:上面的代码实际上有一个错误。 For example, 2 in base 2**7 will get converted to 10 in binary.例如, 2在基座2**7将被转换到10中的二进制。 But digits in base 2**7 should have 7 bits, so you need to pad it to that many bits: 0000010 .但是基数2**7数字应该有7位,因此您需要将其填充到那么多位: 0000010 I'll leave this as an exercise.我将把它留作练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向由数字数组表示的数字加1 - add 1 to number represented by array of digits 什么是'大数字,表示为十进制数字数组'。 它是数组中数字的总和吗? - What is a 'large number, represented as an array of decimal digits'. Is it the sum of the numbers in the array? 改善“增加数字表示的数字”的时间复杂性 - Improving Time Complexity of “increment the number represented as an array of digits” 从输入数字中删除k位后如何获得最少的数字 - How to get the least number after deleting k digits from the input number 如何从数组中获取表示最多的对象 - How to get the most represented object from an array 删除偶/奇数以找到二进制表示的数字数组中的缺失数字 - Removing even/odd numbers to find missing number in an array of array of numbers represented in binary 从其他基数转换后查找数字位数的方法 - Method to find number of digits after converting from a different base number 如何将字母数字电话号码转换为数字 - How to convert an alphanumeric phone number to digits 从二进制数中提取ODD数字的最佳方法 - Best way to extract ODD digits from a binary number 将数字字符串转换为以二进制表示的字节数组 - Converting a Numerical String to an Array of Bytes Represented in Binary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM