How many bits do i need to store AB+C?

219 views Asked by At

I was wondering about this-

If A, B are 16-bit numbers and C is 8-bit, how many bits would I need to store the result ? 32 or 33 ?

And, what if C was a 16-bit number? What then ?

I would appreciate if I got answers with an explanation of the hows and whys.

1

There are 1 answers

2
barak manos On

Why don't you just take the maximum value for each register, and check the result?

If all registers are unsigned:

0xFFFF * 0xFFFF + 0xFF = 0xFFFE0100 = // 32 bits are enough

0xFFFF * 0xFFFF + 0xFFFF = 0xFFFF0000 // 32 bits are enough

If all registers are signed, then 0xFFFF = -32767, but 0xFFFF * 0xFFFF would be the same as before (negative * negative = positive). Register C will make the result a little smaller than the previous result, but you would still require 32 bits in order to store it.