13 Replies - 312 Views - Last Post: 13 October 2019 - 08:55 AM

#1 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Bits manipulation

Posted 12 October 2019 - 01:26 AM

Hi ;
I have this term (((a-B)>>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-B)>>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-B)>>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-B)>>3) + c-d+4)<<2 .


thanks !

Is This A Good Question/Topic? 0
  • +

Replies To: Bits manipulation

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2389
  • View blog
  • Posts: 4,523
  • Joined: 30-May 10

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.
Was This Post Helpful? 1
  • +
  • -

#3 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

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?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2389
  • View blog
  • Posts: 4,523
  • Joined: 30-May 10

Re: Bits manipulation

Posted 12 October 2019 - 08:35 AM

Two 8-bit numbers added together only need 9 bits.
Was This Post Helpful? 1
  • +
  • -

#5 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

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 ..
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,414
  • Joined: 21-June 11

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.
Was This Post Helpful? 1
  • +
  • -

#7 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

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 ..
Was This Post Helpful? 0
  • +
  • -

#8 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

Re: Bits manipulation

Posted 13 October 2019 - 01:53 AM

View PostSalem_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.


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 ..
Was This Post Helpful? 0
  • +
  • -

#9 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2389
  • View blog
  • Posts: 4,523
  • Joined: 30-May 10

Re: Bits manipulation

Posted 13 October 2019 - 02:35 AM

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

// 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

Was This Post Helpful? 0
  • +
  • -

#10 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

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!
Was This Post Helpful? 0
  • +
  • -

#11 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2389
  • View blog
  • Posts: 4,523
  • Joined: 30-May 10

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.
Was This Post Helpful? 0
  • +
  • -

#12 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

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?
Was This Post Helpful? 0
  • +
  • -

#13 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2389
  • View blog
  • Posts: 4,523
  • Joined: 30-May 10

Re: Bits manipulation

Posted 13 October 2019 - 07:19 AM

*sigh*
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.
Was This Post Helpful? 0
  • +
  • -

#14 FreddoMayano   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 11-October 19

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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1