0

Given the following formula :

(((a-b)>>3) + c-d+4)<<2 

And each variable is a 8 bit register, need to find a few bits need for results will not be OVERFLOW .

I think the answer is 11 .

the biggest number that we can take at (a-b) + the biggest number in (c-d+4) .

its bona be grate if someone can explain .

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Yinon Haver
  • 11
  • 1
  • 4
  • Is the question: Given signed integers `a`, `b`, `c`, `d` each in [-128,127], what is the minimum number of bits N required such that neither the *final* result nor any of the *intermediate* results will overflow [-2**(N-1), 2**(N-1)-1], and where `>>` denotes a signed (arithmetic) right shift. – njuffa Jan 16 '17 at 21:43

1 Answers1

0

I think you are on the right track, we shoud try to make a-b and c-d+4 as big as possible, so we set a=255, b=0, c=255, and d=0.

The shift left operator part results in (((a-b)>>3)=31 (5 bits).

And c-d+4=259 (need 9 bits here).

31+259=290(so here we need 9 bits).

And finally (290<<2)=1160 we shift left 2 times, and end up needing 11 bits, which could store a value up to 2047.

J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33