# No of bits necessary for the expression

Status
Not open for further replies.

#### sun_ray How many bits ore necessary for L

L=MN + PQ + 2T

where M,N,Q,T are 16 bits and P is 8 bits.

#### elecrom

##### Junior Member level 3 L will need 33 Bits (Assuming they are unsigned numbers.)

L (bits) = Max ( 32bits , 24bits , 17bits ) + 1 (for carry)

Regards,
-Elecrom

Last edited by a moderator:

#### sun_ray L will need 33 Bits (Assuming they are unsigned numbers.)

L (bits) = Max ( 32bits , 24bits , 17bits ) + 1 (for carry)

Regards,
-Elecrom

Can you please explain how you arrived this? How one carry will be sufficient?

Regards

#### jbeniston Signed or unsigned? Will make a difference for the multiplies.

#### sun_ray Signed or unsigned? Will make a difference for the multiplies.

For both signed and unsigned.

##### Super Moderator
Staff member Signed requires less bits, so the number of bits given by #2 is the required number of bits.

Using a slightly different set of bit widths so it's easy to calculate stuff....
L=MN + PQ + 2T
where M,N,Q,T are 4 bits and P is 2 bits.

M*N is (4-bit)*(4-bit) unsigned the biggest 4-bit number is 15 (i.e. 0b1111) and 15*15 = 225 = 0xE1 (an 8-bit value) so multiplication adds the bit widths up...

therefore P*Q = (4-bit)*(2-bit) = 6-bit (i.e. 15*3)

multiplication by 2 is a shift in binary so 2*T is 5-bit. (i.e. 2*15)

When summing the total can exceed the maximum value of 0xFF, e.g. 225+45+30=300 (a 17-bit number, 0x12C)

So in this case L must be 17-bits.

#### KlausST

##### Super Moderator
Staff member Hi,

L=MN + PQ + 2T

where M,N,Q,T are 16 bits and P is 8 bits.

Unsigned:

M= 16 bits, N = 16 bits therfore MxN = 32 bits.
P = 8 bits, Q = 16 bits, therfore PxQ = 24 bits.
T = 16 bits, therfore 2 x T = 17 bits.

adding a 17 bit value to a 24 bit value give max a 25 bit value (one carry bit on a 24 bit value)

adding a 25 bit value to a 32 bit value gives max a 33 bit value (one carry bit on a 32 bit value)

Klaus

#### xenos

##### Full Member level 4 In order to have a visible illustration of what we are talking about...

Code:
#include <stdio.h>

void xbits(unsigned long long x){
unsigned long long m = 1ull << (sizeof(m)*8-1);
while(m){
putchar((x & m) ? '1':'0');
m>>=1;
}
}

int main(void){
unsigned long long m,n,p,q,t,r;
m = n = t = q = 0xffff;
p = 0xff;
//r = (unsigned long long)-1;
//printf("sizeof(r) == %u, r=%llx\n",sizeof r,r);
r = m*n + p*q + 2*t;
printf("%llx = ",r);
xbits(r);
return 0;
}

results printed:
Code:
100feff00 = 0000000000000000000000000000000100000000111111101111111100000000

33 bits max value (or 32 bits + 1 extra bit)

#### dave_59 You need to be careful which language you use to write the expression as they all have different rules for width determination. T^he only reason you will get 33 bits is because the '2' in 2T is by default a 32-bit number.

Status
Not open for further replies.