Addition for 'short' unsigned integer types

Discussion in 'C Programming' started by badc0de4@gmail.com, Jun 15, 2008.

  1. Guest

    Isn'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.
    , Jun 15, 2008
    #1
    1. Advertising

  2. On Sun, 15 Jun 2008 11:26:20 -0700 (PDT), 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
    Barry Schwarz, Jun 15, 2008
    #2
    1. Advertising

  3. On Jun 15, 7:53 pm, Barry Schwarz <> 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.
    Tomás Ó hÉilidhe, Jun 15, 2008
    #3
  4. Guest

    Barry Schwarz wrote:
    > On Sun, 15 Jun 2008 11:26:20 -0700 (PDT), 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/.
    , Jun 15, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    34
    Views:
    1,368
    Kelsey Bjarnason
    Jan 1, 2004
  2. Replies:
    4
    Views:
    818
    Kaz Kylheku
    Oct 17, 2006
  3. fancyerii
    Replies:
    21
    Views:
    1,488
    Roedy Green
    Nov 5, 2007
  4. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    674
    Eric Sosman
    Mar 5, 2008
  5. pozz
    Replies:
    12
    Views:
    738
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page