Addition for 'short' unsigned integer types

B

badc0de4

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.
 
B

Barry Schwarz

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
 
T

Tomás Ó hÉilidhe

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.
 
B

badc0de4

Barry said:
No. Check section 6.3.1 of the standard, subsections 1 and 8.


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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top