存储 M 个 n 位二进制数之和的最大位宽
Maximum bit-width to store a summation of M n-bit binary numbers
我正在尝试找到计算包含 M 个 n 位无符号二进制数之和所需的最大位宽的公式。谢谢!
所需的最大位宽应为ceil(log_2(M * (2^n - 1)))
。
编辑:感谢@MBurnham,我现在意识到它应该是 floor(log_2(M * (2^n - 1))) + 1
。
假设是正整数,你需要floor(log2(x)) + 1位来存储x。 m 个 n 位数字的总和可以产生的最大值是 m * 2^n。
所以我认为公式应该是
floor(log2(m * 2^n)) + 1
位。
如果我添加 2 个数字,我需要比这 2 个数字中较宽的数字多 1 位来存储结果。所以,如果我添加 2 个 n 位数字,我需要 n+1 位来存储结果。
if I add another n-bit number, I need (n+1)+1 bits to store the result (that's 3 n-bit numbers added so far)
if I add another n-bit number, I need ((n+1)+1)+1 bits to store the result (that's 4 n-bit numbers added so far)
if I add another n-bit number, I need (((n+1)+1)+1)+1 bits to store the result (that's 5 n-bit numbers added so far)
所以,我认为你的公式是
n + M - 1
我正在尝试找到计算包含 M 个 n 位无符号二进制数之和所需的最大位宽的公式。谢谢!
所需的最大位宽应为。ceil(log_2(M * (2^n - 1)))
编辑:感谢@MBurnham,我现在意识到它应该是 floor(log_2(M * (2^n - 1))) + 1
。
假设是正整数,你需要floor(log2(x)) + 1位来存储x。 m 个 n 位数字的总和可以产生的最大值是 m * 2^n。 所以我认为公式应该是
floor(log2(m * 2^n)) + 1
位。
如果我添加 2 个数字,我需要比这 2 个数字中较宽的数字多 1 位来存储结果。所以,如果我添加 2 个 n 位数字,我需要 n+1 位来存储结果。
if I add another n-bit number, I need (n+1)+1 bits to store the result (that's 3 n-bit numbers added so far)
if I add another n-bit number, I need ((n+1)+1)+1 bits to store the result (that's 4 n-bit numbers added so far)
if I add another n-bit number, I need (((n+1)+1)+1)+1 bits to store the result (that's 5 n-bit numbers added so far)
所以,我认为你的公式是
n + M - 1