Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Addition for 'short' unsigned integer types

Reply
Thread Tools

Addition for 'short' unsigned integer types

 
 
badc0de4@gmail.com
Guest
Posts: n/a
 
      06-15-2008
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.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      06-15-2008
On Sun, 15 Jun 2008 11:26:20 -0700 (PDT), http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      06-15-2008
On Jun 15, 7:53*pm, Barry Schwarz <(E-Mail Removed)> 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.
 
Reply With Quote
 
badc0de4@gmail.com
Guest
Posts: n/a
 
      06-15-2008
Barry Schwarz wrote:
> On Sun, 15 Jun 2008 11:26:20 -0700 (PDT), (E-Mail Removed) 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 19
...
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/.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Portable list of unsigned integer types Fred Zwarts C++ 15 10-17-2009 09:03 PM
Standard integer types vs <stdint.h> types euler70@gmail.com C Programming 163 01-28-2008 03:21 PM
conversion of signed integer to unsigned integer junky_fellow@yahoo.co.in C Programming 14 06-18-2005 02:29 PM
unsigned short addition/subtraction overflow Andy C Programming 34 01-01-2004 01:57 AM



Advertisments