- **C Programming**
(*http://www.velocityreviews.com/forums/f42-c-programming.html*)

- - **Addition for 'short' unsigned integer types**
(*http://www.velocityreviews.com/forums/t620343-addition-for-short-unsigned-integer-types.html*)

Addition for 'short' unsigned integer typesIsn't it supposed that the addition 'works' modulo (TYPE_MAX + 1)?
~~~~~~~~~~ unsigned char a, b unsigned char c; a = b = 200; c = a + b; /* OK, c now has 144 */ ~~~~~~~~~~ unsigned char a, b; size_t j; a = b = 200; j = a + b; /* oops, j now has 400 */ ~~~~~~~~~~ For completeness sake, I got across this behavior when coding my version of the CypherSaber RC4 implementation ( http://ciphersaber.gurus.com/faq.html#getrc4 ). My version uses mostly uint8_t variables (from <stdint.h>), and was failing miserably. I tracked it down to the fact that the sum of two uint8_t variables is not necessarily a uint8_t value. ~~~~~~~~~~ unsigned char a, b; size_t j; a = b = 200; j = (unsigned char)(a + b); /* ok again, j now has 144 */ ~~~~~~~~~~ My question again: isn't it supposed that summing two values of a specific type results in a value of that same type? I found nothing that indicated otherwise on pages 42 to 46 of K&R. |

Re: Addition for 'short' unsigned integer typesOn Sun, 15 Jun 2008 11:26:20 -0700 (PDT), badc0de4@gmail.com wrote:
>Isn't it supposed that the addition 'works' modulo (TYPE_MAX + 1)? Yes it is for unsigned addition but you have to have the correct TYPE. > >~~~~~~~~~~ >unsigned char a, b >unsigned char c; >a = b = 200; Integers of rank lower than int will be converted to int (or possibly unsigned int) before the arithmetic operation is performed. >c = a + b; /* OK, c now has 144 */ The sum (400) is coerced into an unsigned char "as if" by modulo arithmetic. >~~~~~~~~~~ >unsigned char a, b; >size_t j; >a = b = 200; >j = a + b; /* oops, j now has 400 */ The sum is still 400 but now it fits in a size_t so the value is unchanged. >~~~~~~~~~~ > >For completeness sake, I got across this behavior when coding my >version of the CypherSaber RC4 implementation ( http://ciphersaber.gurus.com/faq.html#getrc4 >). >My version uses mostly uint8_t variables (from <stdint.h>), and was >failing miserably. >I tracked it down to the fact that the sum of two uint8_t variables is >not necessarily a uint8_t value. > >~~~~~~~~~~ >unsigned char a, b; >size_t j; >a = b = 200; >j = (unsigned char)(a + b); /* ok again, j now has 144 */ The sum is still 400 but is converted to unsigned char before being assigned to j. >~~~~~~~~~~ > >My question again: isn't it supposed that summing two values of a >specific type results in a value of that same type? No. Check section 6.3.1 of the standard, subsections 1 and 8. >I found nothing that indicated otherwise on pages 42 to 46 of K&R. Yes you did. You just need to read it more carefully. Page 44 refers you to Section 6 of Appendix A for the precise rules. This tells you that pages 42-46 are only a summary. A6.1 and A6.5 are consistent with the sections in the standard. Remove del for email |

Re: Addition for 'short' unsigned integer typesOn Jun 15, 7:53*pm, Barry Schwarz <schwa...@dqel.com> wrote:
> Integers of rank lower than int will be converted to int (or possibly > unsigned int) before the arithmetic operation is performed. Just to expand on this: if INT_MAX >= USHRT_MAX unsigned short promotes to signed int else unsigned short promotes to unsigned int if INT_MAX >= UCHAR_MAX unsigned char promotes to signed int else unsigned char promotes to unsigned int All the signed types smaller than int always promote to signed int. Plain char on its own might be signed or unsigned. |

Re: Addition for 'short' unsigned integer typesBarry Schwarz wrote:
> On Sun, 15 Jun 2008 11:26:20 -0700 (PDT), badc0de4@gmail.com wrote: > >isn't it supposed that summing two values of a > >specific type results in a value of that same type? > > No. Check section 6.3.1 of the standard, subsections 1 and 8. > > >I found nothing that indicated otherwise on pages 42 to 46 of K&R. > > Yes you did. You just need to read it more carefully. Page 44 refers > you to Section 6 of Appendix A for the precise rules. This tells you > that pages 42-46 are only a summary. A6.1 and A6.5 are consistent > with the sections in the standard. Thank you. A6.5 (page 198) ... Otherwise, both operands have type int. And A6.1 (page 197) If an int can represent all the values of the original type, then the value is converted to int; This process is called /integral promotion/. |

All times are GMT. The time now is 02:25 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.