Maribov Maribov - 1 month ago 9
C Question

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

Or do I have to account for another variable n that is the result of the problem?

I'm having some trouble thinking this out.

Answer

The number of bits required is n + n. Taking an example using 8 bits and maximum unsigned values:

255 * 255 + 255 = 65280

The result is less then 65536 which would require more than 16 bits.