Hi ;

I have this term (((a->>3) + c-d+4)<<2 which for every variable a,b,c,d is a register of 8bits, how many bits we should have in order that the result of this term "(((a->>3) + c-d+4)<<2 " will not be OVERFLOW .

what's confusing me that we can say about a-b so we should have 8bits for this result, for c-d+4 we should have also 8bits for this result, so in total we should have 16bits in order to not get OVERFLOWD in the output of (((a->>3) + c-d+4)<<2 , I'm not absolutely sure about that so that's why I'm posting here my question, could anyone please help me to figure out how many bits we should have in order to not getting OVERFLOW in the result of this term ((a->>3) + c-d+4)<<2 .

thanks !

# Bits manipulation

Page 1 of 1## 13 Replies - 312 Views - Last Post: 13 October 2019 - 08:55 AM

##
**Replies To:** Bits manipulation

### #2

## Re: Bits manipulation

Posted 12 October 2019 - 04:43 AM

You should edit your post and uncheck the emoticon option, so your post becomes readable.

> every variable a,b,c,d is a register of 8bits

Are these signed or unsigned.

Assume a = c = 0xff

Assume b = d = 0x00

Now work out the expression and count the bits.

> every variable a,b,c,d is a register of 8bits

Are these signed or unsigned.

Assume a = c = 0xff

Assume b = d = 0x00

Now work out the expression and count the bits.

### #3

## Re: Bits manipulation

Posted 12 October 2019 - 05:09 AM

Hi salem, I'm not succeeding to edit my post so I RE-WRITE it here:

this is the term (((a-b>>3) + c-d+4)<<2

didn't get you while you said assume a=c=0xff , b=d=0x00 ; assume that they are unsigned ..

what I think is the result for a-b needs 8bit, the result of c-d+4 needs 8bit so in total we need 16bits in order to not get overflow ..right?

this is the term (((a-b>>3) + c-d+4)<<2

didn't get you while you said assume a=c=0xff , b=d=0x00 ; assume that they are unsigned ..

what I think is the result for a-b needs 8bit, the result of c-d+4 needs 8bit so in total we need 16bits in order to not get overflow ..right?

### #4

## Re: Bits manipulation

Posted 12 October 2019 - 08:35 AM

Two 8-bit numbers added together only need 9 bits.

### #5

## Re: Bits manipulation

Posted 12 October 2019 - 09:34 AM

Hi, may explain to me why? two 8 bit numbers added together so we needs 8bit in the result for not getting "OVERFLOW" in the addition ..

### #6

## Re: Bits manipulation

Posted 12 October 2019 - 09:59 AM

The largest (unsigned) 8-bit number is 255. So the largest result of adding two 8-bit numbers would be 255+255=510. 510 (binary: 111111110) fits in 9 bits. Therefore 9 bits are enough to store the result of adding any two 8-bit numbers without overflow.

### #7

## Re: Bits manipulation

Posted 12 October 2019 - 11:14 AM

I got you ! so for the term (((a-b>>3) + c-d+4)<<2 we should have (9bits+9bits) = 18bits right? for not getting overflow in the result of this term ..

### #8

## Re: Bits manipulation

Posted 13 October 2019 - 01:53 AM

Salem_c, on 12 October 2019 - 04:43 AM, said:

You should edit your post and uncheck the emoticon option, so your post becomes readable.

> every variable a,b,c,d is a register of 8bits

Are these signed or unsigned.

Assume a = c = 0xff

Assume b = d = 0x00

Now work out the expression and count the bits.

> every variable a,b,c,d is a register of 8bits

Are these signed or unsigned.

Assume a = c = 0xff

Assume b = d = 0x00

Now work out the expression and count the bits.

I said it gives me 9+9 bits , so in total 18bits .. am I right? I don't know if the question is hard or I'm not on the proper direction of thinking ..

### #9

## Re: Bits manipulation

Posted 13 October 2019 - 02:35 AM

No you're not right, not by a long shot.

Do you see what's happening yet?

// Two-bit example 11 +11 === 110

// Four-bit example 1111 +1111 ===== 11110

Do you see what's happening yet?

This post has been edited by **Salem_c**: 13 October 2019 - 02:36 AM

### #10

## Re: Bits manipulation

Posted 13 October 2019 - 02:50 AM

GOT YOU!

thanks alot , I see that I must take the extreme case which all bits are one so we will have one more bit than the regular bit size of the input, I mean if 111 + 111 then we need four bits ...

so to sum up, we must need 9+1bit + (9+1bit) => 20 bit!

thanks alot , I see that I must take the extreme case which all bits are one so we will have one more bit than the regular bit size of the input, I mean if 111 + 111 then we need four bits ...

so to sum up, we must need 9+1bit + (9+1bit) => 20 bit!

### #11

## Re: Bits manipulation

Posted 13 October 2019 - 03:08 AM

> so to sum up, we must need 9+1bit + (9+1bit) => 20 bit!

WRONG again.

10 bit number + 10 bit number is 11 bit result.

WRONG again.

10 bit number + 10 bit number is 11 bit result.

### #12

## Re: Bits manipulation

Posted 13 October 2019 - 04:31 AM

Sorry it's obvious that I understand the concept but mistaken on the example that I attached ..

So for this term (((a-b>>3) + c-d+4)<<2 we must have 11 bit in the result in order to not get overflow? but what's confusing me the shifting by 3 and 2 isn't affecting the number of bits?

So for this term (((a-b>>3) + c-d+4)<<2 we must have 11 bit in the result in order to not get overflow? but what's confusing me the shifting by 3 and 2 isn't affecting the number of bits?

### #13

## Re: Bits manipulation

Posted 13 October 2019 - 07:19 AM

*sigh*

a = 0xff

b = 0

c = 0xff

d = 0

This 9-bit result gets shifted left two spaces, giving you 11 bits.

> but what's confusing me the shifting by 3 and 2 isn't affecting the number of bits?

Right shift might not, but left will in this scenario.

a = 0xff

b = 0

c = 0xff

d = 0

// So... 00011111 (a-b>>3) + 11111111 (c-d) 100011110 + 00000100 4 100100010

This 9-bit result gets shifted left two spaces, giving you 11 bits.

> but what's confusing me the shifting by 3 and 2 isn't affecting the number of bits?

Right shift might not, but left will in this scenario.

### #14

## Re: Bits manipulation

Posted 13 October 2019 - 08:55 AM

so in total in order to not get overflow in this term: (((a-b>>3) + c-d+4)<<2 we must have 11 bits yeah?

Page 1 of 1