[英]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.