- **C Programming**
(*http://www.velocityreviews.com/forums/f42-c-programming.html*)

- - **unsigned short addition/subtraction overflow**
(*http://www.velocityreviews.com/forums/t316658-unsigned-short-addition-subtraction-overflow.html*)

unsigned short addition/subtraction overflowHi,
Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1 - 3; TIA Andy |

Re: unsigned short addition/subtraction overflowOn 2003-12-21, Andy <bikejog@hotmail.com> wrote:
> Are 1 through 4 defined behaviors in C? > > unsigned short i; > unsigned long li; /* 32-bit wide */ > > 1. i = 65535 + 3; > 2. i = 1 - 3; > 3. li = (unsigned long)0xFFFFFFFF + 3; > 4. li = 1 - 3; Yes. -- James |

Re: unsigned short addition/subtraction overflowJames Hu wrote:
> > On 2003-12-21, Andy <bikejog@hotmail.com> wrote: > > Are 1 through 4 defined behaviors in C? > > > > unsigned short i; > > unsigned long li; /* 32-bit wide */ > > > > 1. i = 65535 + 3; > > 2. i = 1 - 3; > > 3. li = (unsigned long)0xFFFFFFFF + 3; > > 4. li = 1 - 3; > > Yes. No. 65536 is an allowable value for INT_MAX. (65535 + 3) would be integer overflow and undefined behavior in that case. -- pete |

Re: unsigned short addition/subtraction overflow"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag news:3FE5809B.2575@mindspring.com... > James Hu wrote: > > > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote: > > > Are 1 through 4 defined behaviors in C? > > > > > > unsigned short i; > > > unsigned long li; /* 32-bit wide */ > > > > > > 1. i = 65535 + 3; > > > 2. i = 1 - 3; > > > 3. li = (unsigned long)0xFFFFFFFF + 3; > > > 4. li = 1 - 3; > > > > Yes. > > No. > 65536 is an allowable value for INT_MAX. > (65535 + 3) would be integer overflow > and undefined behavior in that case. From N869: 6.2.5 9 The range of nonnegative values of a signed integer type is a subrange of the corresponding unsigned integer type, and the representation of the same value in each type is the same.28) A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type. However, I am not sure about i = 1 - 3; and li = 1 - 3; but I think it is defined as far as I understand the integer promotion rules. cheers Robert |

Re: unsigned short addition/subtraction overflowpete wrote:
> James Hu wrote: > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote: > > > > > Are 1 through 4 defined behaviors in C? > > > > > > unsigned short i; > > > unsigned long li; /* 32-bit wide */ > > > > > > 1. i = 65535 + 3; > > > 2. i = 1 - 3; > > > 3. li = (unsigned long)0xFFFFFFFF + 3; > > > 4. li = 1 - 3; > > > > Yes. > > No. > 65536 is an allowable value for INT_MAX. > (65535 + 3) would be integer overflow > and undefined behavior in that case. Good catch. However, change "would" to "could". -- Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address! |

Re: unsigned short addition/subtraction overflowRobert Stankowic wrote:
> > "pete" <pfiland@mindspring.com> schrieb im Newsbeitrag > news:3FE5809B.2575@mindspring.com... > > James Hu wrote: > > > > > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote: > > > > Are 1 through 4 defined behaviors in C? > > > > > > > > unsigned short i; > > > > unsigned long li; /* 32-bit wide */ > > > > > > > > 1. i = 65535 + 3; > > > > 2. i = 1 - 3; > > > > 3. li = (unsigned long)0xFFFFFFFF + 3; > > > > 4. li = 1 - 3; > > > > > > Yes. > > > > No. > > 65536 is an allowable value for INT_MAX. > > (65535 + 3) would be integer overflow > > and undefined behavior in that case. > > From N869: 6.2.5 > 9 The range of nonnegative values of a > signed integer type is a subrange of > the corresponding unsigned integer type, > and the representation of the same value in each > type is the same.28) > A computation involving unsigned operands can never overflow, > because a result that cannot be represented by the resulting unsigned > integer type is > reduced modulo the number that is one greater than > the largest value that can be > represented by the resulting type. What's your point ? If INT_MAX equals 65535, then there are no unsigned operands in (65535 + 3), just two ints. > However, I am not sure about > i = 1 - 3; > and > li = 1 - 3; > but I think it is defined as far as I > understand the integer promotion rules. The standard defines what happens when any integer is the right operand of the assignment operator, and the left operand is an unsigned type. -- pete |

Re: unsigned short addition/subtraction overflowCBFalconer wrote:
> > pete wrote: > > James Hu wrote: > > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote: > > > > > > > Are 1 through 4 defined behaviors in C? > > > > > > > > unsigned short i; > > > > unsigned long li; /* 32-bit wide */ > > > > > > > > 1. i = 65535 + 3; > > > > 2. i = 1 - 3; > > > > 3. li = (unsigned long)0xFFFFFFFF + 3; > > > > 4. li = 1 - 3; > > > > > > Yes. > > > > No. > > 65536 is an allowable value for INT_MAX. > > (65535 + 3) would be integer overflow > > and undefined behavior in that case. > > Good catch. However, change "would" to "could". I think I got that right. "in that case" meaning the case when 65536 was equal to INT_MAX, then (65535 + 3) most definitely would overflow, and definitely be undefined behavior. The particular form of the manifestation of the undefined behavior, would be different matter, if that's what you mean. -- pete |

Re: unsigned short addition/subtraction overflowpete wrote:
> > Robert Stankowic wrote: > > > > "pete" <pfiland@mindspring.com> schrieb im Newsbeitrag > > news:3FE5809B.2575@mindspring.com... > > > James Hu wrote: > > > > > > > > On 2003-12-21, Andy <bikejog@hotmail.com> wrote: > > > > > Are 1 through 4 defined behaviors in C? > > > > > > > > > > unsigned short i; > > > > > unsigned long li; /* 32-bit wide */ > > > > > > > > > > 1. i = 65535 + 3; > > > > > 2. i = 1 - 3; > > > > > 3. li = (unsigned long)0xFFFFFFFF + 3; > > > > > 4. li = 1 - 3; > > > > > > > > Yes. > > > > > > No. > > > 65536 is an allowable value for INT_MAX. > > > (65535 + 3) would be integer overflow > > > and undefined behavior in that case. > > > > From N869: 6.2.5 > > 9 The range of nonnegative values of a > > signed integer type is a subrange of > > the corresponding unsigned integer type, > > and the representation of the same value in each > > type is the same.28) > > A computation involving unsigned operands can never overflow, > > because a result that cannot be represented by the resulting unsigned > > integer type is > > reduced modulo the number that is one greater than > > the largest value that can be > > represented by the resulting type. > > What's your point ? > If INT_MAX equals 65535, I meant 65536. > then there are no unsigned operands in (65535 + 3), just two ints. > > > However, I am not sure about > > i = 1 - 3; > > and > > li = 1 - 3; > > but I think it is defined as far as I > > understand the integer promotion rules. > > The standard defines what happens when any integer > is the right operand of the assignment operator, > and the left operand is an unsigned type. -- pete |

Re: unsigned short addition/subtraction overflowbikejog@hotmail.com (Andy) wrote in message news:<aed59298.0312202231.78bb5fae@posting.google. com>...
> Hi, > Are 1 through 4 defined behaviors in C? > > unsigned short i; > unsigned long li; /* 32-bit wide */ > > 1. i = 65535 + 3; > 2. i = 1 - 3; > 3. li = (unsigned long)0xFFFFFFFF + 3; > 4. li = 1 - 3; > > TIA > Andy Actually what I really meant is for unsigned operations. This is what I want to know. Are the following defined in C and always guaranteed warped around values? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = (unsigned short)65535 + (unsigned short)3; 2. i = (unsigned short)1 - (unsigned short)3; 3. li = (unsigned long)0xFFFFFFFF + (unsigned long)3; 4. li = (unsigned long)1 - (unsigned long)3; TIA Andy |

Re: unsigned short addition/subtraction overflow"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag news:3FE5ADF9.1044@mindspring.com... > pete wrote: > > > > Robert Stankowic wrote: > > > > > > "pete" <pfiland@mindspring.com> schrieb im Newsbeitrag [....] > > What's your point ? > > If INT_MAX equals 65535, > > I meant 65536. > > > then there are no unsigned operands in (65535 + 3), just two ints. I see your point. Thanks Robert |

All times are GMT. The time now is 01:40 PM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.