Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   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)

badc0de4@gmail.com 06-15-2008 06:26 PM

Addition for 'short' unsigned integer types
 
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.

Barry Schwarz 06-15-2008 06:53 PM

Re: Addition for 'short' unsigned integer types
 
On 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

Tomás Ó hÉilidhe 06-15-2008 07:16 PM

Re: Addition for 'short' unsigned integer types
 
On 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.

badc0de4@gmail.com 06-15-2008 07:19 PM

Re: Addition for 'short' unsigned integer types
 
Barry 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.