简体   繁体   English

表示已知大小变量的表达式结果的最小位数?

[英]Minimum number of bits to represent the result of an expression on variables of known size?

I'm trying to learn more about how computers store memory. 我正在尝试更多地了解计算机如何存储内存。 So I've found information on how to represent binary numbers or hexadecimal numbers, but I found a question asking: 所以我找到了有关如何表示二进制数或十六进制数的信息,但我发现了一个问题:

Given three n-bit unsigned ints: x, y, z, what is the min # 
of bits required to represent x * y + z 

So my thinking is: Do I have to just account for all three ints separately and do the math for the smallest possible numbers so for example 0 * 0 + 0 所以我的想法是:我是否必须分别考虑所有三个整数并对最小可能的数字进行计算,例如0 * 0 + 0

Or do I have to account for another variable n that is the result of the problem? 或者我是否必须考虑问题导致的另一个变量n?

I'm having some trouble thinking this out. 我在思考这个问题时遇到了一些麻烦。

The number of bits required is n + n . 所需的位数是n + n Taking an example using 8 bits and maximum unsigned values: 以8位和最大无符号值为例:

255 * 255 + 255 = 65280

The result is less then 65536 which would require more than 16 bits. 结果小于65536 ,这将需要超过16位。

Max value of an n-bit unsigned number is 2 n - 1. n位无符号数的最大值是2 n -1。

The max result of 最大的结果

a*b + c a * b + c

would be: 将会:

(2 n - 1)*(2 n - 1) + (2 n - 1) (2 n - 1)*(2 n - 1)+(2 n - 1)

(2 2n - 2 n - 2 n + 1) + (2 n - 1) (2 2n - 2 n - 2 n + 1)+(2 n - 1)

2 2n - 2 n 2 2n - 2 n

Therefore, since 因此,自此

2 2n - 2 n 2 2n - 2 n

is less than the maximum (2*n)-bit unsigned number: 2 2n - 1, (2*n) bits will suffice to represent any answer. 小于最大(2 * n)位无符号数:2 2n - 1,(2 * n)位足以代表任何答案。

Examples of how to code it 如何编码的例子

uint64_t ab_plus_c_bad(uint32_t a, uint32_t b, uint32_t c) {
  return a*b + c;  // potential overflow with product.
}

uint64_t ab_plus_c_good(uint32_t a, uint32_t b, uint32_t c) {
  // a is widened before the multiplication
  return (uint64_t) a*b + c;  // no product overflow
}

The maximum value that can be represented is pow(2,n)-1. 可以表示的最大值是pow(2,n)-1。 and your equation yields (pow(2,n)-1) * (pow(2,n)-1) + (pow(2,n)-1) 并且你的方程得到(pow(2,n)-1)*(pow(2,n)-1)+(pow(2,n)-1)

So to find out how many bits are required 所以要找出需要多少位

n |max val.| max eqn.   | req'd bits 
--|--------|------------|-----------
1 |   1    |  1*1+1=2   | 2
2 |   3    |  3*3+3=12  | 4
3 |   7    |  7*7+7=56  | 6
4 |   15   |15*15+15=240| 8
5 |   31   |31*31+31=992| 10
...see the pattern

To figure this out for any n, use something like this: 为了弄清楚这一点,对任意 n,使用这样的:

//This uses double since the loss of precision will be insignificant
//but covers a large range of n.
//There are more efficient ways for small values of n.
//long double could be used instead for REALLY large values of n
#include <math.h>
#define n 999UL

//the highest unsigned value that can be represented by n bits is:
double max_value = pow(2.0, (double)n) - 1.0;

//figure out the max value you can get with your equation
double max_eqn = max_value * max_value + max_value;

//take the log base 2 of that and round it up to get max bits
//note that log base x of n is equal to log(n)/log(x);
unsigned long req_bits = (unsigned long) ceil(log(max_eqn)/log(2.0));

printf("The maximum required number of bits for %ul bits is %ul", n, req_bits);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM