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