On Wed, 4 May 2011, Kevin Bailey wrote:

On Wed, May 4, 2011 at 7:31 PM, Erik Petrich
<epetr...@ivorytower.norman.ok.us> wrote:

  long_counter = (unsigned int)(char_counter[0] | (char_counter[1] << 8)) |
     ((unsigned long)(char_counter[2] | (char_counter[3] << 8)) << 16);

I hope the first (unsigned int) cast isn't necessary, because the
char_counter[1] << 8 would overflow if it wasn't implicitly an int.

Yes, char_counter[1] << 8 is implicitly an int. The cast is needed to force it to be unsigned. Otherwise during conversion from (signed) int to unsigned long the most significant bit of the int result will be copied to all of the bits in the upper half of the long, which could then interfere with the bytes from char_counter[2] and char_counter[3]. Converting from unsigned int to unsigned long will force all the bits in the upper half of the long to 0, so no interference. My original suggestion omitted this cast and failed because I forgot to take this signed int to unsigned long behaviour into account.

  Erik
------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network 
management toolset available today.  Delivers lowest initial 
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to